Explain Codes LogoExplain Codes Logo

Find which version of package is installed with pip

python
pip-management
package-versions
python-3.8
Anton ShumikhinbyAnton Shumikhin·Dec 20, 2024
TLDR

To check a specific package's version with pip:

pip show <package_name> | grep Version

For just the package's version number:

pip list | grep <package_name>

In scenarios where the above methods don't work or in older versions of pip, use this command:

pip freeze | grep <package_name>

Checking versions programmatically

You can also check the package version inside your Python script. Here's how:

  • For Python versions 3.8 and above, use the importlib.metadata module:
from importlib.metadata import version # Who needs a fortune cookie when you have a version? print(version('<package_name>'))
  • Running an older version of Python? Fear not. Use the importlib-metadata package instead:
from importlib_metadata import version # Just like asking your old, wise mentor for advice print(version('<package_name>'))

Clearing up inconsistencies

Stumbling upon discrepancies between packages? Keep these in mind:

  • Confirm naming conventions. Some packages have different names on PyPI than the ones used in import statements.
  • If using a mix of pip and conda, double check if different versions of the same package are installed.

Maintaining a clean environment

To prevent unexpected behavior from old packages:

  • Do routine clean-ups of the site-packages directory or use virtual environments to keep everything tidy.
  • The pip list --outdated command will become your trusted companion in identifying out-of-date packages.

Becoming a guru in package management

Take your proficiency to another level with pip-tools:

  • Dive into pip-tools for a more robust handle on package management and compilation of requirements.
  • Lock package versions using pip-compile to avoid any compatibility issues with future updates.

Handling multiple environments

While dealing with multiple environments, it's good practice to:

  • Cross-check package versions across your development, testing, and production environments.
  • Use requirements.txt files generated by pip-tools to manage dependencies per environment.

Keeping up with the pip vibes

Stay up-to-date with pip enhancements:

  • Visit the pip GitHub repository often to explore the evolution of pip commands.
  • Regularly check the official Python Packaging Authority updates to be cognizant with the new features of pip.