pip package-installation version-control Python tutorial

Installing specific package version with pip

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Pip, which stands for "Pip Installs Packages," is the package manager for Python used to install, upgrade, and manage Python packages and their dependencies. While pip makes it easy to get the latest versions of software, there are times when you'll want to install a specific version of a package, either because of compatibility issues or to match the environment used for another application.

Why Pin Package Versions?

Compatibility

Often, newer versions of a package might change or remove features that your code relies on. Specifying package versions can ensure long-term compatibility and prevent your projects from unexpectedly breaking due to future upgrades of dependent libraries.

Stability

Using a specific version helps maintain stable behavior in production environments. Software is inherently complex, and new versions may introduce bugs or regressions.

Reproducibility

For scientific research or auditing, you might need the exact environment that was used at the original time of experimentation. Pinning package versions allows anyone to recreate the same setup.

Understanding Version Notation

Python packages follow Semantic Versioning, denoted as MAJOR.MINOR.PATCH.

  • MAJOR: Increases when there are incompatible API changes.
  • MINOR: Updates when new functionality is added in a backward-compatible way.
  • PATCH: Increases when backward-compatible bug fixes are made.

Installing a Specific Package Version

To install a specific version of a package, pip provides an option to specify the version. The syntax for this is pip install package_name==version_number.

bash
pip install numpy==1.21.2

This command installs version 1.21.2 of the numpy package, overriding any other version if installed already.

Installing Multiple Specific Versions

If you have multiple packages to install at specific versions, you can list them in a requirements.txt file.

Example requirements.txt:

 
numpy==1.21.2
pandas==1.3.3
scipy==1.7.1

To install all the packages listed in requirements.txt, use:

bash
pip install -r requirements.txt

Using Inequality Operators

Pip also allows version specification using inequality operators like <, <=, >, >=, and !=. These operators help in scenarios where a specific version constraint is necessary but not limited to a single version.

For instance, if you need a version of a package that is 1.0 or higher but not 2.0, the command would be:

bash
pip install pkg_name>=1.0,<2.0

Upgrading/Downgrading to Specific Versions

To upgrade/downgrade a package to a specific version, you can use:

bash
pip install --upgrade package_name==version_number

Exploring Installed Packages and Their Versions

To check which versions of packages are installed, the pip list command can be used. This is crucial for verifying the current setup.

bash
pip list 

This command outputs a list of all installed packages along with their current versions. Another helpful command is pip freeze, which is especially useful for creating a snapshot of installed packages into a requirements.txt file:

bash
pip freeze > requirements.txt

Additional Tools for Version Management

Virtual Environments

Using virtual environments can create isolated spaces in which packages and their specific versions are installed, thus not interfering with the system-wide Python installation. Popular tools for creating virtual environments include venv and virtualenv.

bash
python3 -m venv myenv
source myenv/bin/activate

Package Installer for Python (PIP) Versions

PIP has its own versions, and occasionally, newer features or bug fixes in pip itself are essential for managing packages effectively. To update pip, use:

bash
pip install --upgrade pip

Summary Table

CommandPurpose
pip install pkg==1.0Install specific version of a package
pip install -r fileInstall from a requirements file
pip listList installed packages along with versions
pip freeze > fileOutput installed packages and versions into a file
pip install op_pkg>=1.0Install package version with condition (e.g., >= 1.0)
python3 -m venv myenvCreate a virtual environment named myenv
source myenv/bin/activateActivate myenv virtual environment

Conclusion

Installing specific package versions using pip is a powerful way to maintain stable and reproducible Python environments. Whether for development, testing, or deployment in production, controlling package versions helps ensure that your code behaves predictably and consistently across different setups.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.