Last Updated : 07 Aug, 2024
Summarize
Comments
Improve
Working on Python projects may require you to list the installed Python packages in order to manage dependencies, check for updates, or share project requirements with others. In this post, we’ll look at numerous techniques for listing the Python packages that are installed on your system.
List Installed Python Packages
Below are some methods by which we can list installed Python Packages:
- Using pip list
- Using pip freeze
- Using Python’s pkg_resources
- Using pipdeptree
- Using pipenv
- Using Jupyter Notebook
List Installed Python Packages using pip list
The most common method for listing installed Python packages is by using the pip command-line tool. pip is the standard package manager for Python, and it comes pre-installed with Python 3.4 and later versions.
We can list installed packages using pip by using the list command. Just open the terminal or command prompt and run the following command and you will see the list of all installed packages.
pip list
This command will display a list of all the Python packages installed in your Python environment and their versions. You can also redirect the output to a text file(Optional) If you want to save the list of installed packages to a text file, you can use the following command:
pip list > installed_packages.txt
This command will create a file named installed_packages.txt and save the list of packages in it.
List Python Packages using pip freeze
Another commonly used command to list installed packages, especially in the context of Python project management, is pip freeze. This command generates a list of installed packages and their versions in a format that is commonly used for specifying project dependencies in a requirements.txt file.
We can use pip freeze to list installed Python packages. Just open your terminal or command prompt and run the following command.
pip freeze
This command will display a list of installed packages and their versions, similar to what you might find in a requirements.txt file.
List Python Packages using Python’s pkg_resources Module
If you prefer a programmatic approach to list installed packages from within a Python script, you can use the pkg_resources module from the setuptools package. Here’s an example of how to use it:
import pkg_resourcesinstalled_packages = pkg_resources.working_setfor package in installed_packages: print(f"{package.key}=={package.version}")
This script will print the names and versions of all installed packages in your Python environment.
List Python Packages using pipdeptree
pipdeptree is a Python package that helps you visualize and list the dependencies of installed packages in a tree-like structure. To use it, you need to install it first:
pip install pipdeptree
After installing pipdeptree, you can list installed packages and their dependencies as follows:
pipdeptree
This command will display a tree-like structure of installed packages and their dependencies.
List Installed Packages using pipenv
pipenv is a popular tool for managing Python project environments and dependencies. It combines pip and virtualenv to create isolated Python environments for your projects. To list installed packages within a pipenv environment, follow these steps:
- Open a terminal or command prompt and navigate to the directory where your Pipfile is located.
- Activate the virtual environment:
pipenv shell
List installed packages:
pipenv lock --requirements
This command will display the list of installed packages and their versions.
Using Jupyter Notebook
If we are using Jupyter Notebook for Python development and want to list installed packages within a notebook, we can do so using the ! (shell command) prefix. Here’s how:
- Open a Jupyter Notebook: Launch a Jupyter Notebook by running jupyter notebook in your terminal.
- Create a new notebook or open an existing one: You can create a new notebook or open an existing one where you want to list the installed packages.
- List installed packages within a notebook cell:
!pip list
How To List Installed Python Packages – FAQs
How do I Get a List of Installed Packages in Python?
To get a list of installed packages in Python, you can use the
pip
command-line tool with thelist
command. This will show all packages installed in the current environment.Example using pip:
pip list
This command prints all the installed packages along with their versions in the active Python environment.
How do I See What Modules are Installed?
Seeing what modules are installed in Python can also be done using
pip list
as mentioned above. Additionally, you can use Python code to list installed packages usingpkg_resources
which is part ofsetuptools
.Example using Python:
import pkg_resourcesinstalled_packages = pkg_resources.working_set
installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages])
for package in installed_packages_list:
print(package)This script will list all installed packages and their versions.
How do I Get a List of Locally Installed Python Modules?
If you want to see locally installed Python modules (not installed via pip), you can list modules available in the local directories. However, standard methods like using
pip
show all modules installed via package managers, including pip. Locally developed modules that are not installed via pip won’t show up inpip list
. For locally developed modules, ensure they are accessible via Python’s path (you can check this usingsys.path
).
Where Does Python Look for Packages?
Python looks for packages in a list of directories defined in
sys.path
. This list usually includes:
- The directory from which the input script was run or the current directory if the interpreter is being run interactively.
- The list of directories contained in the
PYTHONPATH
environment variable, if it is set.- Installation-dependent default directories (which can be checked by looking at
sys.path
).Example to check
sys.path
:import sys
print(sys.path)
How to Install a List of Python Packages from a Text File?
To install a list of Python packages from a text file (commonly named
requirements.txt
), you can usepip
with the-r
option. This is commonly used to manage dependencies in Python projects.Example:
pip install -r requirements.txt
The
requirements.txt
file should contain the names of the packages possibly with versions specified, one per line like this:numpy==1.19.2
pandas>=1.1.3
matplotlib!=3.1.1This approach allows you to maintain consistent environments across different setups, such as development and production environments, by ensuring that the same versions of all required packages are installed.
Next Article
How to Check If Python Package Is Installed