To install a specific version of a Python package using pip
, use the following command format:
pip install package_name==version_number
Examples:
- Install an exact version:
pip install requests==2.25.1
- Version ranges:
- Minimum version:
bash pip install "requests>=2.25.0"
- Maximum version:
bash pip install "requests<=2.26.0"
- Range between versions:
bash pip install "requests>=2.25.0,<3.0.0"
- Compatible releases (
~=
):
Allows updates within the same major/minor version (e.g.,~=2.25.0
means>=2.25.0
and<2.26.0
):
pip install "requests~=2.25.0"
- Force reinstallation (if the package is already installed):
pip install --force-reinstall requests==2.25.1
- Install from a Git repository, branch, or tag:
pip install git+https://github.com/user/repo.git@v1.2.3
- Install from a
.whl
(wheel) file or URL:
pip install https://example.com/packages/package-1.0.4-py3-none-any.whl
- Using a
requirements.txt
file:
Add the package and version to a file (e.g.,requirements.txt
):
requests==2.25.1
numpy>=1.20.0
Then run:
pip install -r requirements.txt
Notes:
- Ensure the version is available on PyPI or the specified source.
- Use
pip search package_name
or check PyPI to verify available versions. - For pre-release versions, include
--pre
:
pip install --pre package_name==2.0.0rc1