43

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.

john-hen
  • 4,410
  • 2
  • 23
  • 40
Sherein
  • 947
  • 1
  • 11
  • 23
  • https://stackoverflow.com/questions/35064426/when-would-the-e-editable-option-be-useful-with-pip-install – Andrew Feb 19 '22 at 01:59

3 Answers3

32

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 = "beliaev.m.s@gmail.com"},
    {name = "Bo Yang", email = "boy@kth.se"},
]
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]
Beliaev Maksim
  • 968
  • 12
  • 21
  • 13
    `pip install .` doesn't just download the dependencies, it *installs* them, as well as the actual project itself. – john-hen Nov 15 '21 at 18:33
  • 1
    true, but maybe this answer will be anyway helpful for somebody. I find limited info on pyproject.toml file – Beliaev Maksim Feb 07 '22 at 09:35
  • 3
    This answer was certainly helpful for me - I couldn't find anywhere else the 'dependencies' list under project was specified, and that's what is needed for pip to install other packages needed at run time. – A. Rose Jul 16 '22 at 22:36
  • This is a great example, and like `@A. Rose` I couldn't find much in the way of examples. However I have one question/problem remaining (and I accept that I may be searching for a tree while stumbling through a forest): how do I install the `project.optional-dependencies` from your example using `pip`? – d3r3kk Oct 26 '22 at 02:16
  • Ha! Answered my own question (I found that dang tree!). From [here](https://github.com/pypa/pip/issues/4340) I deduced that installing an `[project.optional-dependencies]` is done like so: `python -m pip install .[dependency-name]`. – d3r3kk Oct 26 '22 at 02:36
  • 3
    If you run Zsh, run `pip install -e '.[tests]'`, if you get the `zsh: no matches found: .[tests]` error. – NostraDavid Feb 05 '23 at 17:07
  • What about listing a local package in the toml `dependencies`. There's a package I need that is not on pypi and I have included its repo in my project. I've tried `"/packagedirectory"` and other similar syntax but none of these are PEP 508 compliant and thus raise an error. Hints? – jacob Mar 03 '23 at 16:19
  • Never mind, I came up with a solution to local dependencies with "relative" absolute paths [here](https://stackoverflow.com/a/75630599/9403538). – jacob Mar 03 '23 at 18:28
8

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 .
-5

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
finswimmer
  • 10,896
  • 3
  • 34
  • 44
  • 9
    The question is about using the pyproject.toml file directly, not generating a requirements.txt file. – A. Rose Jul 16 '22 at 22:31
  • The OP said they are using Poetry. So this is the answer to do it. If you really want to have only one command, the answer would be: You cannot. – finswimmer Feb 16 '23 at 06:22
  • The OP asks specifically about using pip to download the dependencies directly using the pyproject.toml file - which pip can do (as it can interpret the given pyproject.toml file). The poetry export is not needed. – A. Rose Feb 17 '23 at 20:13
  • The OP said they are **not** using poetry. But I agree it is confusing, because [tag:python-poetry] is tagged. – rv.kvetch May 26 '23 at 19:11