Find which version of package is installed with pip

Learn find which version of package is installed with pip with practical examples, diagrams, and best practices. Covers python, pip development techniques with visual explanations.

How to Find the Version of a Python Package Installed with Pip

Hero image for Find which version of package is installed with pip

Learn various methods to quickly determine the installed version of any Python package using pip, the standard package installer for Python.

When working with Python projects, it's crucial to know which version of a particular package is installed in your environment. This information is vital for debugging, ensuring compatibility, replicating environments, and managing dependencies. Pip, Python's package installer, provides several straightforward ways to retrieve this information. This article will guide you through the most common and effective methods.

Method 1: Using pip show

The pip show command is the most direct and recommended way to get detailed information about an installed package, including its version. It provides a comprehensive summary, which is often more useful than just the version number alone.

pip show <package_name>

Basic syntax for pip show

For example, to find the version of the requests library, you would run:

pip show requests

Example of pip show for the 'requests' package

The output will include the Version: field, along with other details like Name, Location, Requires, and Required-by.

Method 2: Using pip freeze and grep (or findstr on Windows)

The pip freeze command outputs a list of all installed packages and their versions in a requirements.txt format. While useful for generating dependency lists, it can be combined with command-line tools like grep (on Linux/macOS) or findstr (on Windows) to filter for a specific package.

# On Linux/macOS
pip freeze | grep <package_name>

# On Windows
pip freeze | findstr <package_name>

Using pip freeze with grep or findstr

For instance, to find the numpy version:

# On Linux/macOS
pip freeze | grep numpy

# On Windows
pip freeze | findstr numpy

Example for the 'numpy' package

This method is particularly useful when you want to quickly scan through your entire environment or when pip show might be too verbose for a simple version check.

Method 3: Programmatic Check within Python

Sometimes, you might need to check a package's version directly from within a Python script. Most well-behaved Python packages expose their version number via a __version__ attribute.

import <package_name>
print(<package_name>.__version__)

Basic syntax for programmatic version check

Let's check the pandas version using this method:

import pandas
print(pandas.__version__)

Example for the 'pandas' package

flowchart TD
    A[Start: Need Package Version] --> B{Is `pip show` sufficient?}
    B -- Yes --> C[Run `pip show <package_name>`]
    C --> D[Extract 'Version:' from output]
    B -- No --> E{Need to list all packages or filter?}
    E -- Yes, list all --> F[Run `pip freeze`]
    F --> G{Filter with `grep` or `findstr`}
    G --> D
    E -- No, programmatic --> H[Import package in Python]
    H --> I{Check `package_name.__version__`}
    I -- Success --> D
    I -- Fail --> J[Consult package docs for version attribute]
    J --> D
    D --> K[End: Version Found]

Decision flow for finding a Python package version

Choosing the right method depends on your specific needs. For a quick, detailed overview of a single package, pip show is ideal. If you're managing dependencies or need to filter a large list, pip freeze combined with a text utility is efficient. For integration into scripts, the programmatic __version__ attribute is the way to go.