How to Install a Specific Version of a Python Package Using pip

Sometimes the latest version of a Python package breaks your project. Other times, your app requires a specific version to work correctly. Instead of guessing, you can install the exact version you need using pip.

How to Install a Specific Version of a Python Package Using pip

This guide shows you how to install a specific version of any Python package, how to downgrade safely, and how to check available versions before installing.

Step 1: Check Your Python and pip Version

Before installing anything, confirm that Python and pip work correctly.

Open Command Prompt or Terminal and run:

python --version
pip --version

If pip does not respond, install or upgrade it first:

python -m ensurepip --upgrade

Step 2: Install a Specific Version Using pip

Use this format:

pip install package_name==version_number

Example

Install a specific NumPy version:

pip install numpy==1.26.4

pip downloads and installs exactly that version.

Step 3: Install a Version Range (Optional)

Sometimes you want flexibility but still need limits. Use comparison operators:

pip install "django>=3.2,<4.0"

This command installs a Django version between 3.2 and 4.0.

Step 4: Downgrade to an Older Version

If a recent update causes issues, install an older version directly:

pip install requests==2.28.1

pip replaces the current version automatically.

If needed, force reinstall:

pip install --force-reinstall requests==2.28.1

Step 5: Upgrade to a Specific Version

To upgrade to a chosen version:

pip install --upgrade pandas==2.1.4

This command updates the package to that exact version.

Step 6: Check Available Versions Before Installing

Before choosing a version, see what versions exist:

pip index versions flask

This command lists all available versions from PyPI.

Step 7: Verify the Installed Version

After installation, confirm the version:

pip show package_name

Example:

pip show numpy

You can also check inside Python:

import numpy
print(numpy.__version__)

How to Use requirements.txt to Lock Python Package Versions

If you manage a project, lock versions inside requirements.txt:

numpy==1.26.4
django==3.2.20
requests==2.28.1

Install everything with:

pip install -r requirements.txt

This method ensures consistent environments across systems.

Best Practice: Use a Virtual Environment

Avoid installing packages globally. Create a virtual environment:

python -m venv venv

Activate it:

Windows

venv\Scripts\activate

macOS/Linux

source venv/bin/activate

Then install your specific version safely inside the environment.

Common pip Errors and How to Fix Them

1. “No matching distribution found”

You likely entered an incorrect version. Run:

pip index versions package_name

2. Permission denied

Add --user:

pip install package_name==version --user

Or run the terminal as administrator.

3. Version conflict

Check installed packages:

pip list

Resolve dependency conflicts manually or create a clean virtual environment.

Installing a specific package version with pip gives you full control over your development environment. Whether you need to downgrade, upgrade, or match production settings, pip makes it simple.

Always verify available versions before installing. Use virtual environments for isolation. Lock versions in requirements.txt for long-term stability.

That approach keeps your Python projects stable, predictable, and production-ready.

Leave a Comment

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply