Manage Python dependencies in four decisions
Dependency work comes down to four separate questions: where packages are installed, what your project asks for, which exact versions were chosen, and what to do when two requirements disagree. This guide takes them in that order. For an import that fails right now, start with checking the interpreter and environment instead.
In this guide
- Manage Python dependencies in four decisions
- One virtual environment per project
- Declare what your project needs
- Version specifiers that mean different things
- pip freeze records, it does not solve
- Constraints limit versions without installing anything
- Reading a ResolutionImpossible error
- What pip check does and does not tell you
- Lock files for repeatable installs
- Check what you download
- A routine for updating dependencies
One virtual environment per project
python3 -m venv .venv
.venv/bin/python -m pip install --upgrade pip
On Windows the interpreter is .venv\Scripts\python.exe. Separate environments let two projects use different versions of the same library. Do not commit or copy the environment itself; recreate it from the files described below. Ubuntu users can follow installing pip and venv on Ubuntu.
Declare what your project needs
List the packages your code imports directly. Their own dependencies (transitive dependencies) are found by the installer. In a pyproject.toml project this looks like the following; see the packaging guide:
[project]
name = "report-tool"
version = "0.1.0"
dependencies = [
"requests>=2.28,<3",
]
[project.optional-dependencies]
excel = ["openpyxl"]
[dependency-groups]
dev = ["pytest"]
optional-dependencies are extras that users of your package can choose. dependency-groups are for development tasks and are not published as extras; pip can install one with python -m pip install --group dev from version 25.1. An application without packaging metadata can simply keep a requirements.txt.
Version specifiers that mean different things
| Specifier | Allows |
|---|---|
==2.28.1 | Only that release |
>=2.28,<3 | 2.28 and later, below 3 |
~=2.28 | 2.28 and later in the 2 series |
~=2.28.0 | 2.28.0 and later in the 2.28 series only |
The last two differ by a single .0; the rule is in the version specifier specification. Libraries should generally allow a range, because an exact pin can make them impossible to install next to other software. Applications should record exact versions in a lock or pinned file.
pip freeze records, it does not solve
python -m pip freeze > requirements.txt
python -m pip install -r requirements.txt
pip freeze prints what is installed in the current environment. It does not distinguish your direct dependencies from transitive ones, it adds no file hashes, and the result is tied to the platform and Python version you ran it on. Run it inside a clean project environment, or it will include unrelated packages.
Constraints limit versions without installing anything
A constraints file says which versions are acceptable if a package gets installed. Listing a package there does not install it.
# requirements.txt
demo-reports
# constraints.txt
demo-core<2
python -m pip install -r requirements.txt -c constraints.txt
This is useful for holding back a transitive dependency without pretending your code imports it.
Reading a ResolutionImpossible error
We reproduced a conflict with small local packages made only for this example (they are not on PyPI): demo-reports requires demo-core<2, and demo-api requires demo-core>=2. Installing both with pip 22.0.2 on Ubuntu 22.04.5 produced:
ERROR: Cannot install demo-api==1.0.0 and demo-reports==1.0.0 because these package versions have conflicting dependencies.
The conflict is caused by:
demo-reports 1.0.0 depends on demo-core<2
demo-api 1.0.0 depends on demo-core>=2
ERROR: ResolutionImpossible
(Hint lines and the documentation link that pip prints are left out here.) Read the block under “The conflict is caused by”: it names the two requirements that cannot both be true. Newer pip versions may word the output differently. The pip resolution guide suggests working through these steps:
- Remove top-level requirements you no longer need.
- Loosen your own pins, for example replace
==with a tested range. - Check whether a newer or older release of one of the conflicting packages accepts a common version, or ask its maintainers.
- If no combination exists, replace one of the packages or separate the two tools into different environments.
What pip check does and does not tell you
In the same lab we installed demo-reports and then forced demo-core 2.1.0 in with --no-deps. python -m pip check exited with status 1:
demo-reports 1.0.0 has requirement demo-core<2, but you have demo-core 2.1.0.
A healthy environment prints No broken requirements found. The command compares declared metadata of installed packages. It does not read your requirements file and does not run any code, so follow it with your tests. Avoid --no-deps as a way around a conflict: the error disappears from the install and reappears when the program runs.
Lock files for repeatable installs
| Tool | Typical commands | Note |
|---|---|---|
| pip-tools | pip-compile, pip-sync | pip-sync also uninstalls what is not listed |
| uv | uv lock, uv sync | Writes uv.lock; sync removes unlisted packages by default |
| Poetry | poetry lock, poetry install, poetry run | poetry shell moved to a plugin; poetry env activate prints the activation command |
| pip lock | python -m pip lock | Experimental; valid for the current Python version and platform |
Pick one and commit its lock file. Using several on the same project makes it unclear which file is true.
Check what you download
pip’s hash-checking mode compares every downloaded file with a recorded hash. Once any requirement has a hash, all of them need one and must be pinned. pip-compile --generate-hashes can write them:
pip-compile --generate-hashes -o requirements.txt pyproject.toml
python -m pip install --require-hashes -r requirements.txt
A matching hash shows the file is the one you recorded, not that the package is safe. Before adding a dependency, check its exact name, project page and maintenance.
A routine for updating dependencies
- Start from a clean environment and a committed lock or pinned file.
- Upgrade one package or one small group.
- Run
python -m pip checkand your tests. - Read the release notes of anything that changed major version.
- Commit the new lock file together with the code change it needs.
