pythondependenciespackagingpyproject.toml

Specify dependencies in pyproject.toml with install URL or with index-url


I like to have my package installable with pip install ... and to use the pyproject.toml standard.

I can specify dependencies to install from git, with:

dependencies = [
    'numpy>=1.21',
    'psychopy @ git+https://github.com/psychopy/psychopy',
]

But how can I specify a dependency to install from a different indexer, equivalent to:

python -m pip install --pre --only-binary :all: -i https://pypi.anaconda.org/scientific-python-nightly-wheels/simple numpy scipy

With or without the pre-release flag?

And how can I specify a dependency to install from a URL, e.g. https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-22.04/wxPython-4.2.1-cp310-cp310-linux_x86_64.whl I tried with no luck:

dependencies = [
    'wxPython @ https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-22.04/wxPython-4.2.1-cp310-cp310-linux_x86_64.whl; python_version == "3.10"; sys_platform == "linux"'
]


Solution

  • I recommend reading the article "install_requires vs. requirements files" in the "Python packaging user guide".

    This article is partly outdated but the principle of abstract dependencies vs. concrete dependencies is unchanged. Concrete dependencies do not belong in packaging metadata, i.e. concrete dependencies can not be added to the dependencies list in the [project] table of the pyproject.toml file.

    As you have noted, it is possible to add "direct references" (for example psychopy @ git+https://github.com/psychopy/psychopy), but not all packaging tools support this and notably PyPI rejects the upload of packages containing such dependencies.

    What I often recommend, is that if there are dependencies that are not on PyPI then the documentation should reflect that very clearly and prominently. Where to get the dependencies and some examples of how to install them should be in the documentation, for example the URLs of the alternative package repositories.

    And another thing that I recommend on top of the documentation is to provide one or more requirements.txt files that contain a list of concrete dependencies that have been tested and are known to work well. For example:

    MyPackage
    psychopy @ git+https://github.com/psychopy/psychopy
    wxPython @ https://extras.wxpython.org/wxPython4/extras/linux/gtk3/ubuntu-22.04/wxPython-4.2.1-cp310-cp310-linux_x86_64.whl; python_version == "3.10"; sys_platform == "linux"
    

    And if I am not mistaken pip can install directly from the URL of such a requirements file: python -m pip install --requirement https://host.example/my-package/requirements.txt so such a command could be added to the documentation as well.

    Related: