Install a specific Python package version with pip

Use exact pins or version ranges in the intended environment, inspect available candidates, and check the complete dependency set after changing versions.

Best practices
Illustration of software components, data, and application workflows

Use python -m pip install "package==version" to request an exact version. A different installed version can be replaced by the same command, including when the requested version is older. You normally do not need to uninstall it first or force a reinstall.

Express the version policy

python -m pip install "requests==2.31.0"
python -m pip install "requests>=2.28,<3.0"
python -m pip install "requests~=2.31.0"
python -m pip install "requests>=2.0"

These are syntax examples using historical versions, not recommendations to deploy those releases. Quote specifiers so the shell does not interpret comparison characters as redirection.

For normal final releases, ~=2.31.0 allows 2.31 patch releases from 2.31.0 onward and excludes 2.32.0. The number of version components matters: ~=2.31 permits later 2.x releases. Python packaging's rules also cover pre-releases, post-releases, and local versions; do not assume another package manager's operator is identical.

Check the environment and available candidates

python -m pip --version
python -m pip index versions requests

The version list reflects the configured index and compatibility filters. It is not necessarily every release published anywhere. Use a virtual environment and the intended Python interpreter before installing.

After changing a version:

python -m pip install "requests==2.31.0"
python -m pip check

pip check detects declared dependency conflicts. It does not test application behavior, optional features, or API compatibility.

Put constraints in a requirements file

requests==2.31.0
flask>=3.0,<4.0
numpy~=1.26.4

Exact direct pins do not fully pin transitive dependencies; ranges can resolve differently later. Reproducible deployment also depends on the interpreter, platform, indexes, and artifacts. A lock-generation workflow, hashes, and retained artifacts can strengthen that process; pip freeze is only a snapshot of one installed environment.

FAQ

Why does pip report No matching distribution found?

Check the package/version spelling, index access, required Python version, platform tags, and binary/source selection options. A compiler error generally occurs later, after pip has selected source to build. A missing compiler is not the generic explanation for “no matching distribution.”

Does --upgrade override an exact pin?

No. The requested version must still satisfy the pin. --upgrade is usually unnecessary for changing the named package to an exact version; it also interacts with dependency-upgrade strategy, so avoid assuming every combination has identical effects on dependencies.

What if another package requires an incompatible version?

Resolve the conflicting requirements together. Tools such as pip-tools, uv, or Poetry can help compute and record a compatible environment, but no lock file can make contradictory requirements compatible. Test the resulting environment before deployment.

References: pip install, Python package version specifiers, pip dependency resolution.

Get a tailored plan

Contact us

Join the community

Scan with WeChat
to join the community

Try Guance

Start online and pay only for what you use.

Get started

Choose a Guance plan

Code hosting