pip
package management
version control
Python
software installation

Find which version of package is installed with pip

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding Package Versioning with pip

Python’s package manager, pip, is an indispensable tool for installing and managing Python packages. When working with multiple packages, especially in complex projects, it's crucial to know which specific versions are installed. This helps ensure compatibility and reliability across development and production environments. Here's a comprehensive guide on how to find out which version of a package is installed using pip.

Checking Installed Package Versions

There are several ways to determine the version of a package installed via pip. Below are some common methods:

Method 1: Using pip show

The simplest way to check the version of an installed package is by using the pip show command. This command displays detailed information about the package, including its version.

bash
pip show package_name

Example:

bash
pip show numpy

This command will return an output like:

 
1Name: numpy
2Version: 1.21.0
3Summary: Fundamental package for array computing in Python.
4...

Method 2: Using pip list

The pip list command shows all installed packages along with their versions. This is particularly useful if you want to see all packages at once.

bash
pip list

Example Output:

 
1Package    Version
2---------- -------
3numpy      1.21.0
4pandas     1.3.0
5requests   2.25.1

Method 3: Using Python Code

You can also check the version of a package directly within a Python script or interactive shell. This approach is useful for verifying versions dynamically within your codebase.

python
import package_name
print(package_name.__version__)

Example:

python
import numpy
print(numpy.__version__)

This should output:

 
1.21.0

Enhancements with pip freeze

The pip freeze command outputs installed packages in a format that allows easy installation in other environments using a requirements file.

bash
pip freeze

Example Output:

 
numpy==1.21.0
pandas==1.3.0
requests==2.25.1

You can direct the output of pip freeze to a requirements.txt file:

bash
pip freeze > requirements.txt

Comparing with Available Versions

Once you know which version is installed, you may want to compare it with available versions to see if an update is needed.

Method 1: Using pip install

To see all available versions of a package, use the pip install command with the package name followed by ==.

bash
pip install package_name==

Example:

bash
pip install numpy==

This will list available versions of numpy:

 
1...
21.19.0
31.20.0
41.21.0
51.22.0
6...

Summarized Key Points

Below is a table summarizing the methods discussed:

CommandDescriptionUse Case
pip showDisplays detailed information about a package, including its version.To check a single package version.
pip listLists all installed packages and their versions.To get an overview of all installed packages.
Python ScriptAccess version via package_name.__version__.To dynamically check a package version within a script.
pip freezeFormats installed packages for replication in other environments.To create requirements.txt file.

Additional Tools and Libraries

  • virtualenv: Create isolated Python environments to manage package dependencies.
  • conda: An alternative package manager which can also provide detailed package information and manage environments.
  • pipdeptree: A command-line utility for displaying installed packages in a tree structure which shows dependencies and versions.

Conclusion

Understanding which versions of packages are installed can help maintain the stability and compatibility of Python applications. By using tools like pip show, pip list, and pip freeze, you can efficiently manage package versions. Being aware of these capabilities enhances software reliability and facilitates smooth transitions between various development phases.


Course illustration
Course illustration

All Rights Reserved.