6

I have a python package that I want to install inside a docker file.

pyproject.toml looks like:

[build-system]
requires = ["setuptools>=61.0"]
build-backend = "setuptools.build_meta"

[project]
name = "bar"
dependencies = [
    "pandas",
]

[project.optional-dependencies]
foo = [
    "matplotlib",
]

... and Dockerfile:

# ...
WORKDIR /app
COPY . /app
RUN pip install /app

This installs the dependencies (in this example, pandas) on every build, which I want to avoid to save developer time, since pyproject.toml is rarely touched.

How can I install only pandas (the dependencies listed pyproject.toml) without having to COPY . and installing bar.

I want to avoid:

  • adopt other tools like poetry
  • create a requirements.txt and use dynamic keyword in pyproject.toml, because I have optional-dependencies and I want to keep the list of dependencies as close together (i.e. same file) as possible.

Something like:

# ...
WORKDIR /app
COPY ./pyproject.toml /app/
RUN pip install --requirements-from /app/pyproject.toml  # <-- HERE
COPY . /app
RUN pip install /app  # <-- Installs `bar` only. All dependencies already installed.
obk
  • 488
  • 5
  • 12
  • https://stackoverflow.com/questions/2317410/pip-installing-only-the-dependencies -- This question has already been asked, why is your question any different? -- Also maybe this has elements of answers: https://discuss.python.org/t/how-to-install-only-dependencies-of-a-package/4027 – sinoroc Nov 08 '22 at 09:37
  • 2
    Thanks for the links. I was searching for a solution that specifically uses pyproject.toml. They do mention pip-tools (`pip-compile -o requirements.txt pyproject.toml`), which I was not aware of, and may work for me. Will give it a try. – obk Nov 09 '22 at 19:40
  • A problem with `pip-compile` as a solution is that by design it locks to an exact version, which may be a problem if the versions of python you are testing cannot all use the same exact version of some package. – Sam Schick Jan 06 '23 at 02:06

0 Answers0