flit icon indicating copy to clipboard operation
flit copied to clipboard

Command to install only dependencies, but not project itself

Open arogozhnikov opened this issue 3 years ago • 5 comments

Scenario:

package heavily uses docker and needs building/rebuilding on every commit.

Typical old way is to have requirements.txt that are passed to docker and installed first, then whole package is installed. This way docker can reuse the cache if dependencies do not change.

Poetry provides --no-root flag for such scenario (sufficient to pass only pyproject.toml to container, much nicer than passing multiple files for several groups of requirements), but in flit I see no way to achieve the same.

arogozhnikov avatar Mar 22 '22 04:03 arogozhnikov

I have proposed a solution for this request here #546 ,

dciborow avatar Apr 27 '22 19:04 dciborow

Thank you @dciborow

I like the idea of having a separate command install-reqs instead of a flag in install. Will track if that's going to be accepted.

arogozhnikov avatar Apr 27 '22 20:04 arogozhnikov

Seconded - This is essential functionality for container-first builds and frankly I think a compelling argument in favor of using requirements.txt and pip over pyproject.toml and flit

zachlipp avatar Jul 26 '22 22:07 zachlipp

For a workaround, I've added the following layers to my Dockerfile:

COPY ./pyproject.toml ./

# Install and cache ALL packages specified in pyproject.toml
# Assumes requirements meet the following constraints:
# 1. Are on lines beginning with leading spaces
# 2. Specify versions (using any of `>`, `<`, and/or `=`)
# 3. Include double quotes around them
# e.g.
#   "fastapi==0.63.0", 
#   "pytest >6.2.4,<7.0.0",

RUN grep -oP '^ *"[\s\S]+?[=><]+[\s\S]+?"' pyproject.toml > requirements.txt && \
    sed -i 's|"||g' requirements.txt && \
    pip install -r requirements.txt

...

RUN flit install ...

flit installs the package per usual but the build times are much faster as Docker is able to cache the dependencies. The regular expression is pretty brittle so YMMV

zachlipp avatar Jul 27 '22 22:07 zachlipp

flit 3.8 adds an --only-deps option which looks like it should work but it still requires the package source code to be available. I was able to work around this by copying README.rst and doing RUN touch $MY_PROJECT_NAME.py but it's a horrible hack.

The following still fails:

COPY pyproject.toml ./
RUN flit install --only-deps

As a point of comparison you can do the following using pdm:

COPY pyproject.toml pdm.lock ./
RUN pdm export -o requirements.txt && pip install -r requirements.txt

cdleonard avatar Nov 08 '22 12:11 cdleonard