Sherein
Sherein

Reputation: 1857

Download dependencies declared in pyproject.toml using Pip

I have a Python project that doesn't contain requirements.txt. But it has a pyproject.toml file.

How can I download packages (dependencies) required by this Python project and declared in pyproject.toml using the Pip package manager (instead of the build tool Poetry).

So instead of pip download -r requirements.txt, something like pip download -r pyproject.toml.

Upvotes: 128

Views: 106421

Answers (9)

CpILL
CpILL

Reputation: 6999

If you want a lighting fast version of pip-tools try uv

uv pip install -r pyproject.toml

or with test extras

uv pip install -r pyproject.toml --extra test

or all the extras

uv pip install -r pyproject.toml --all-extras

or to spit out an old requirements.txt file and sync deps

uv pip compile pyproject.toml > requirements.txt
uv pip sync -r requirements.txt

OR! If you want to go all in with uv:

uv sync

Which will create its own lock file, and .venv/ and activate it, and install the dependencies in the pyproject.toml file.

Upvotes: 23

elig
elig

Reputation: 3078

An "indirect" way to do it using yq:

$ yq '.project.dependencies[]' pyproject.toml | xargs -r pip install

This will extract the list of dependencies in pyproject.toml file and if there are any it will feed them to the pip install command

Upvotes: 1

Beliaev Maksim
Beliaev Maksim

Reputation: 1589

Here is an example of .toml file:

[build-system]
requires = [
    "flit_core >=3.2,<4",
]
build-backend = "flit_core.buildapi"

[project]
name = "aedttest"
authors = [
    {name = "Maksim Beliaev", email = "[email protected]"},
    {name = "Bo Yang", email = "[email protected]"},
]
readme = "README.md"
requires-python = ">=3.7"
classifiers = ["License :: OSI Approved :: MIT License"]
dynamic = ["version", "description"]

dependencies = [
    "pyaedt==0.4.7",
    "Django==3.2.8",
]

[project.optional-dependencies]
test = [
    "black==21.9b0",
    "pre-commit==2.15.0",
    "mypy==0.910",
    "pytest==6.2.5",
    "pytest-cov==3.0.0",
]

deploy = [
    "flit==3.4.0",
]

to install core dependencies you run:

pip install .

if you need test(develop) environment (we use test because it is a name defined in .toml file, you can use any):

pip install .[test]

To install from Wheel:

pip install C:\git\aedt-testing\dist\aedttest-0.0.1-py3-none-any.whl[test]

Upvotes: 80

Doron Behar
Doron Behar

Reputation: 2878

Based on pip-tools and the command this pip issue (and this):

python -m pip install \
  --no-deps \
  --requirement <(python -m piptools \
    compile \
    --output-file - 2>/dev/null \
  )

--no-deps is used because all dependencies are already recursively added to the requirements list by piptools' command. You can add a sed filter to the output of the inner piptools command to filter out packages if you need to, like here:

python -m pip install \
  --no-deps \
  --requirement <(python -m piptools \
    compile \
    --output-file - 2>/dev/null | sed \
      -e 's/^numpy==1.26.4/#\0 is not really needed/g' \
  )

Upvotes: 0

Marvin
Marvin

Reputation: 3045

The comment by @andrew (When would the -e, --editable option be useful with pip install?) is a clue but I'll make it explicit. Simply doing

pip install -e .

will install the dependencies. It also "installs" the project as an "editable install". That installs only metadata about the current project - it doesn't generate build output or a whl, and changes in the source files are immediately reflected in the "installed" version.

Upvotes: 6

sunyata
sunyata

Reputation: 2251

If you want to download the dependencies you can do the following:

  1. cd into the directory where the pyproject.toml file is contained
  2. pip download .

Upvotes: 7

Daniel Deng
Daniel Deng

Reputation: 470

For whoever is looking for a simple answer with only pip, this worked for me:

pip install . && pip uninstall -y <your-project>

E.g. I have a Dockerfile looks like this:

ADD pyproject.toml /workspaces/my-awesome-project/

RUN pip install . && \
    pip uninstall -y my-awesome-project && \
    rm pyproject.toml

Upvotes: 18

seasonedfish
seasonedfish

Reputation: 333

pip supports installing pyproject.toml dependencies natively.

As of version 10.0, pip supports projects declaring dependencies that are required at install time using a pyproject.toml file, in the form described in PEP 518. When building a project, pip will install the required dependencies locally, and make them available to the build process. Furthermore, from version 19.0 onwards, pip supports projects specifying the build backend they use in pyproject.toml, in the form described in PEP 517.

From the project's root, use pip's local project install:

python -m pip install .

Upvotes: 17

finswimmer
finswimmer

Reputation: 15192

You can export the dependencies to a requirements.txt and use pip download afterwards:

poetry export -f requirements.txt > requirements.txt
pip download -r  requirements.txt

Upvotes: 3

Related Questions