Search code examples
pythonlibrariesdependency-management

Python - specify what library version to use (for all of them)


How to list in Python project all libraries and their version?
(similar to Java maven pom.xml and Node.js npm 'package.json')

There is definitely way to specify that on command line,
e.g. pip install pandas==0.23, but that only hopefully will be listed in README, but most likely be forgotten.

Also as there is pip and conda, do they address this? Or maybe some other 3rd tool tries to unify for whatever pip or conda usage?


Solution

  • There are two main mechanisms to specify libraries for a package/repository:

    • For an out-of-the-box package that can be installed with all dependencies, use setup.py/pyproject.toml. These are used by package managers to automatically install required dependencies on package installation.
    • For a reproducible installation of a given setup of possibly many packages, use requirements.txt. These are used by package managers to explicitly install required dependencies.

    Notably, the two handle different use-cases: Package dependencies define the general dependencies, only restricting dependency versions to specific ranges when needed for compatibility. Requirements define the precise dependencies, restricting all direct and indirect dependency versions to replicate some pre-existing setup.