6 Effective Ways to Check the Version of a Python Package in Linux?

Written by

Reviewed by

Last updated: July 24, 2024

Expert verified

SVG Image

TL;DR

Here are the three ways to check the version of a Python package in Linux:

  1. pip Command: Use pip show | grep Version to display the package version (e.g., pip show requests | grep Version).
  2. pip freeze Command: Run pip freeze to list all installed packages and their versions.
  3. pip list Command: Type pip list to review all installed packages and their versions.

Learn all about checking the version of a Python package in Linux with this article here, including expert tips and troubleshooting strategies.

Knowing the version of a Python package installed on your Linux system is essential for avoiding compatibility issues, security risks, and other problems. Luckily, there are easy solutions. In this post, I’ll show you different ways to check the version of a Python package in Linux, troubleshoot common errors, and keep your development environment running smoothly. You’ll learn how to use commands like pip list, write simple Python scripts, and use system package managers to find package versions quickly.

Importance of Knowing the Version of a Python Package

Understanding the version of a Python package in your Linux environment is crucial for several reasons. Here’s an overview of why it’s important:

  • Compatibility: Ensures different parts of a software project work together seamlessly by managing dependencies and maintaining project consistency.
  • Security: Protects data and applications from vulnerabilities by applying security patches and complying with security standards.
  • Performance: Optimizes application performance with enhancements and bug fixes included in newer versions of packages.
  • Feature Availability: Utilizes the latest features and functionalities offered by updated packages, enhancing development and application capabilities.
  • Debugging and Maintenance: Simplifies the process of identifying and resolving issues, with easier access to relevant documentation and community support.

How to Check the Version of a Python Package in Linux

To check the version of a Python package in Linux, you can use several methods. First, you can use the pip command by typing pip show <package_name> | grep Version in the terminal, which displays the package version. Another method is the pip freeze command, which lists all installed packages and their versions when you run pip freeze in the terminal.

You can also use the pip list command by simply typing pip list, which shows all installed packages with their versions. Finally, you can write a Python script to check the version by importing the package and printing package_name.__version__. Save and run the script in the terminal to see the package version.

Let’s explore each of these four above-mentioned methods and two more methods to check the version of a Python package in Linux here:

1. pip Command

pip is a popular package manager for Python that can be used to install, update, and manage Python packages. Here’s how you can check the version of a Python package using pip in the Terminal:

  1. In the command prompt, execute the following command:
pip show requests | grep Version 
  1. Now, you’ll see the Python package version number installed on your Linux system.
check the version of a python package

2. pip freeze Command

The pip freeze command is a powerful tool for Python developers that allows them to display a list of all installed packages and their versions. This can be extremely useful for keeping track of dependencies and ensuring that your code is using the correct versions of each package. Follow these steps to use the pip freeze command:

  1. Launch the Terminal app, and run the following command:
pip freeze
  1. The Terminal will display a list of all installed packages and their versions. You can scroll through till the end to see more details.
pip freeze installed packages and their versions

3. pip list Command

The pip list command lists all installed Python packages along with their versions. This method is useful when you need to review all the packages in your environment.

  1. Open your terminal emulator.
  2. Simply type the following command and press Enter:
pip list

The terminal will display a list of all installed packages and their versions. Look for the package you are interested in.

viewing list of packages and their version

4. Python Script

You can also check the version of a package from within a Python script by using the import package-name module and package-name.version attribute. Here’s how to do it:

  1. In the text editor, type the following code:
import package-name

print(package-name.__version__)
text editor import package name
  1. Save the script with .py extension.
save the script with py extension
  1. Head to the Terminal app, navigate to the directory with the cd command, and run the saved Python script.
run the saved python script
  1. The Terminal app will display the version number of the package.
want to check the version of a python package

5. Python Interpreter

Another way to check the version of a Python package directly is in the Python interpreter. This is useful when you just need to have a quick look into the version number of a package without having to create a separate script for it. Here’s how you can do it:

  1. In the Linux command prompt, execute the following command to open the Python interpreter:
python3
command to open the python interpreter
  1. Once the Python interpreter opens, type the following code:
import package-name

package-name.__version__
python interpreter opens import package name
  1. Press Enter. Then, the Python interpreter will import the package and display its version.
check the version of a python package interpreter

6. Python Script with pkg_resources

Writing a simple Python script using pkg_resources allows you to programmatically check the version of a package. This method is beneficial when you need to check versions within a script or application.

  1. Open your text editor and write the following script:
import pkg_resources

package = 'numpy'

version = pkg_resources.get_distribution(package).version

print(f'{package} version: {version}')
writing a script in text editor
  1. Save the file as check_version.py.
saving the script file
  1. In your terminal, navigate to the directory where the script is saved and run:
python3 check_version.py

The script will print the version of the specified package.

viewing version of the specified package using python script

How to Check the Python Package Version on Different Linux Distribution

Different Linux distributions have different package managers and their versions. So, they have slightly different commands to check them. Here are some ways to check the version of a Python package for a specific Linux distribution:

  • Across all distributions: To check the Python version across all distributions, you can use platform.python_version() in Python code.
  • Debian-based distributions: For Debian-based distributions, such as Ubuntu, use apt-cache policy package_name to check the version of a package.
  • Fedora-based distributions: For Fedora-based distributions, such as Red Hat and CentOS, use dnf info package_name
  • Arch Linux: For Arch Linux, use the pacman -Qi package_name command to check the package version. 

Common Issues and Troubleshooting in Python Package Management

Managing Python packages in Linux can sometimes present challenges. Here are common issues you might encounter and straightforward solutions to resolve them, ensuring a smooth development process.

1. Package Not Found: You try to check the version of a package, but the system can’t find it.

  • Solution: Make sure the package is installed. You can install it using pip install <package_name> or conda install <package_name>. If you are in a virtual environment, ensure it’s activated.

2. Version Mismatch: You notice different versions of the same package in different environments.

  • Solution: Check which environment is currently active. Use pip list or conda list to verify the versions. Align the package versions across environments using pip install <package_name>==<version>.

3. Network Issues: You face network errors while checking or installing packages.

  • Solution: Ensure you have an active internet connection. If you’re behind a firewall or proxy, configure your network settings. You can set up a proxy in pip using pip --proxy <proxy_url> install <package_name>.

4. Permission Issues: You get permission errors when checking or installing packages.

  • Solution: Use sudo to run commands with administrative privileges: sudo pip install <package_name>. Alternatively, use virtual environments to manage packages without requiring admin rights.

5. Corrupted Package: The package seems corrupted or doesn’t work correctly after installation.

  • Solution: Uninstall the package with pip uninstall <package_name> and reinstall it. If the problem persists, clear the cache using pip cache purge before reinstalling.

6. Incompatible Package: The installed package version conflicts with other dependencies in your project.

  • Solution: Use virtual environments to isolate project dependencies. Create a virtual environment with python -m venv <env_name>, activate it, and then install your packages. This ensures that your project’s dependencies do not interfere with each other.

Key Takeaway

To wrap up, checking the version of a Python package in Linux is crucial for maintaining compatibility, security, performance, and feature availability in your projects. Whether you use commands like pip list or pip show, Python scripts, or system package managers, knowing how to troubleshoot common issues will greatly enhance your development process and keep your environment stable.

If you’re interested in learning more, I suggest you dive into articles on:

    Frequently Asked Questions

    How do I check the version of a package if it’s not installed on my system?

    To check the version of a package, if it’s not installed on your system, use the apt-cache search command. In the Terminal app, type apt-cache search package-name and replace the package-name with the name of the package. For example, apt-cache search requests will display a list of packages that match the search term requests and their versions.

    What are Python packages, and how do they work?

    Python packages are pre-built modules of code that provide additional functionality to Python programs. They can include modules, libraries, and frameworks and can be installed using package managers like pip, apt, or dnf based on the Linux distro. Python packages can be either standard library packages, third-party packages, or local packages created by a user or a team.

    Can different versions of Python packages be installed simultaneously?

    Yes, different versions of Python packages can be installed simultaneously using virtual environments. Virtual environments allow you to create an isolated environment with its own Python executable and package dependencies. This allows you to have multiple versions of the same package installed without causing compatibility issues.

    How are Python package versions numbered, and what do the numbers represent?

    Python package versions are numbered according to a specific system consisting of three parts separated by dots: major version, minor version, and patch version. The major version indicates a significant change to the package that is not backward compatible with previous versions, while the minor version signifies a smaller change that is backward compatible. The patch version denotes a bug fix or minor improvement to the package.

    Ojash

    Author

    Ojash is a skilled Linux expert and tech writer with over a decade of experience. He has extensive knowledge of Linux's file system, command-line interface, and software installations. Ojash is also an expert in shell scripting and automation, with experience in Bash, Python, and Perl. He has published numerous articles on Linux in various online publications, making him a valuable resource for both seasoned Linux users and beginners. Ojash is also an active member of the Linux community and participates in Linux forums.

    Akshat

    Reviewer

    Akshat is a software engineer, product designer and the co-founder of Scrutify. He's an experienced Linux professional and the senior editor of this blog. He is also an open-source contributor to many projects on Github and has written several technical guides on Linux. Apart from that, he’s also actively sharing his ideas and tutorials on Medium and Attirer. As the editor of this blog, Akshat brings his wealth of knowledge and experience to provide readers with valuable insights and advice on a wide range of Linux-related topics.

    Share this article
    Shareable URL
    Prev Post

    3 Best Ways to Prettify JSON in Linux

    Next Post

    How to Use bash set x in Linux [4 Practical Examples]

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    Read next