Use the double-equals operator == (pip install package_name==version) to install an exact version of a Python package, or use version comparison specifiers (>=, <, ~=) wrapped in quotes to install within a controlled version range.
Basic Example
# 1. Install an exact version of a package
pip install requests==2.31.0
# 2. Recommended: invoke pip through your active Python interpreter
python -m pip install requests==2.31.0
# 3. Install multiple pinned packages at once
pip install "numpy==1.26.4" "pandas==2.2.1"
Key Details & Version Specifiers
- Always quote range operators in your shell: Characters like
<andare interpreted by Bash, Zsh, PowerShell, and Command Prompt as file redirection operators. Wrap specifiers in double quotes (e.g.,pip install "django>=4.2,<5.0") so the shell passes them directly topip. - Compatible release operator (
~=): Usingpip install "flask~=3.0.2"installs the latest patch release in the3.0.xseries (>=3.0.2, ==3.0.*) without jumping to3.1.0—ideal for receiving bug and security fixes without breaking changes. - Downgrading an already-installed package: Running
pip install requests==2.28.0when2.31.0is already installed automatically uninstalls the newer version and installs2.28.0—no--force-reinstallflag is required unless the target version is already installed and corrupted. - Checking available versions before installing: Run
pip index versions <package_name>(available inpip21.2+) to list all available versions on PyPI directly from your terminal:pip index versions requests - Pinning versions in
requirements.txt: Record exact installed versions for reproducible builds usingpip freeze > requirements.txt, then install them elsewhere withpip install -r requirements.txt.
Common pip Version Specifiers
| Specifier | Example | Meaning | Best Used When |
|---|---|---|---|
== |
requests==2.31.0 |
Exact version match only | Production requirements.txt files requiring strict reproducibility. |
~= |
requests~=2.31.0 |
Compatible release (>=2.31.0, ==2.31.*) |
Allowing safe patch updates while locking minor/major versions. |
>= / <= |
"requests>=2.28,<3.0" |
Bounded minimum and maximum version range | Reusable libraries supporting multiple compatible releases. |
!= |
"requests>=2.28,!=2.30.0" |
Excludes a specific broken or buggy release | Skipping a known yanked or regression release. |
You must log in or register to comment.
