TL;DR
Here are the three ways to check the version of a Python package in Linux:
- pip Command: Use pip show | grep Version to display the package version (e.g., pip show requests | grep Version).
- pip freeze Command: Run pip freeze to list all installed packages and their versions.
- 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:
- In the command prompt, execute the following command:
pip show requests | grep Version
- Now, you’ll see the Python package version number installed on your Linux system.

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:
- Launch the Terminal app, and run the following command:
pip freeze
- The Terminal will display a list of all installed packages and their versions. You can scroll through till the end to see more details.

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.
- Open your terminal emulator.
- 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.

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:
- In the text editor, type the following code:
import package-name
print(package-name.__version__)

- Save the script with .py extension.

- Head to the Terminal app, navigate to the directory with the cd command, and run the saved Python script.

- The Terminal app will display the version number of the 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:
- In the Linux command prompt, execute the following command to open the Python interpreter:
python3

- Once the Python interpreter opens, type the following code:
import package-name
package-name.__version__

- Press Enter. Then, the Python interpreter will import the package and display its version.

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.
- 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}')

- Save the file as check_version.py.

- 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.

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>
orconda 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
orconda list
to verify the versions. Align the package versions across environments usingpip 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 usingpip 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:
- Methods for checking None values in Python to improve your debugging skills and maintain clean, error-free code.
- Setting up Python3 on Ubuntu to ensure you have the correct setup from the start.
- Installing Anaconda on Linux to streamline your package management.
Frequently Asked Questions
How do I check the version of a package if it’s not installed on my system?
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.