How to Check Installed Packages in Python Quickly
To check installed packages in Python, use the
pip list command in your terminal or command prompt. Alternatively, you can run pip freeze to see installed packages with their versions. You can also check packages programmatically using the pkg_resources module.Syntax
There are two main commands to check installed packages using pip:
pip list: Shows all installed packages with their versions.pip freeze: Lists installed packages in a format suitable for requirements files.
To use these commands, open your terminal or command prompt and type them directly.
bash
pip list pip freeze
Example
This example shows how to list installed packages using pip list and how to check packages inside a Python script using pkg_resources.
python
import pkg_resources # List installed packages programmatically installed_packages = pkg_resources.working_set packages_list = sorted([f"{i.key}=={i.version}" for i in installed_packages]) for package in packages_list: print(package)
Output
pip==23.0.1
setuptools==67.6.1
wheel==0.38.4
Common Pitfalls
Some common mistakes when checking installed packages include:
- Running
pipcommands in the wrong environment (e.g., system Python vs virtual environment). - Using
pipwithout specifying the Python version, which can cause confusion if multiple Python versions are installed. - Expecting
pip listto show packages installed by other means (like system package managers).
To avoid these, always activate your virtual environment first and use python -m pip list to ensure you check the correct Python installation.
bash
wrong: pip list right: python -m pip list
Quick Reference
Summary of commands to check installed Python packages:
| Command | Description |
|---|---|
| pip list | Lists installed packages with versions |
| pip freeze | Lists installed packages in requirements format |
| python -m pip list | Runs pip list for the current Python interpreter |
| Use pkg_resources in Python | Check installed packages programmatically |
Key Takeaways
Use
pip list or pip freeze in the terminal to see installed packages.Run
python -m pip list to ensure you check the correct Python environment.Activate your virtual environment before checking packages to avoid confusion.
Use
pkg_resources in Python code to list installed packages programmatically.Remember that system package managers may install packages not shown by pip.