- pip list#
- Options#
- Examples#
- Check available packages python
- # Table of Contents
- # Check if a Python package is installed
- # Installing the module if it isn’t already installed
- # Check if a Python package is installed using find_spec
- # Installing the package if it isn’t already installed
- # Checking if the module isn’t installed
- # Additional Resources
- How to List Installed Python Packages
- List Installed Packages with Pip
- List Packages in a Console with Pip
- List Modules in a Console without Pip
- List Installed Packages with Pipenv
- List Installed Packages with Anaconda Navigator
- List Installed Packages with Conda
- Globally vs Locally Installed Packages
- List Installed Packages with the ActiveState Platform
pip list#
List installed packages, including editables. Packages are listed in a case-insensitive sorted order.
Options#
-o , —outdated # List outdated packages -u , —uptodate # List uptodate packages -e , —editable # List editable projects. -l , —local # If in a virtualenv that has global access, do not list globally-installed packages. —user # Only output packages installed in user-site. —path # Restrict to the specified installation path for listing packages (can be used multiple times). —pre # Include pre-release and development versions. By default, pip only finds stable versions. —format # Select the output format among: columns (default), freeze, or json. The ‘freeze’ format cannot be used with the —outdated option. —not-required # List packages that are not dependencies of installed packages. —exclude-editable # Exclude editable package from output. —include-editable # Include editable package from output. —exclude # Exclude specified package from the output -i , —index-url # Base URL of the Python Package Index (default https://pypi.org/simple). This should point to a repository compliant with PEP 503 (the simple repository API) or a local directory laid out in the same format. —extra-index-url # Extra URLs of package indexes to use in addition to —index-url. Should follow the same rules as —index-url. —no-index # Ignore package index (only looking at —find-links URLs instead). -f , —find-links # If a URL or path to an html file, then parse for links to archives such as sdist (.tar.gz) or wheel (.whl) files. If a local path or file:// URL that’s a directory, then look for archives in the directory listing. Links to VCS project URLs are not supported.
Examples#
$ python -m pip list Package Version ------- ------- docopt 0.6.2 idlex 1.13 jedi 0.9.0
C:\> py -m pip list Package Version ------- ------- docopt 0.6.2 idlex 1.13 jedi 0.9.0
$ python -m pip list --outdated --format columns Package Version Latest Type ---------- ------- ------ ----- retry 0.8.1 0.9.1 wheel setuptools 20.6.7 21.0.0 wheel
C:\> py -m pip list --outdated --format columns Package Version Latest Type ---------- ------- ------ ----- retry 0.8.1 0.9.1 wheel setuptools 20.6.7 21.0.0 wheel
$ python -m pip list --outdated --not-required Package Version Latest Type -------- ------- ------ ----- docutils 0.14 0.17.1 wheel
C:\> py -m pip list --outdated --not-required Package Version Latest Type -------- ------- ------ ----- docutils 0.14 0.17.1 wheel
$ python -m pip list --format=json [, , .
C:\> py -m pip list --format=json [, , .
$ python -m pip list --format=freeze colorama==0.3.7 docopt==0.6.2 idlex==1.13 jedi==0.9.0
C:\> py -m pip list --format=freeze colorama==0.3.7 docopt==0.6.2 idlex==1.13 jedi==0.9.0
When some packages are installed in editable mode, pip list outputs an additional column that shows the directory where the editable project is located (i.e. the directory that contains the pyproject.toml or setup.py file).
$ python -m pip list Package Version Editable project location ---------------- -------- ------------------------------------- pip 21.2.4 pip-test-package 0.1.1 /home/you/.venv/src/pip-test-package setuptools 57.4.0 wheel 0.36.2
C:\> py -m pip list Package Version Editable project location ---------------- -------- ---------------------------------------- pip 21.2.4 pip-test-package 0.1.1 C:\Users\You\.venv\src\pip-test-package setuptools 57.4.0 wheel 0.36.2
The json format outputs an additional editable_project_location field.
$ python -m pip list --format=json | python -m json.tool [ "name": "pip", "version": "21.2.4", >, "name": "pip-test-package", "version": "0.1.1", "editable_project_location": "/home/you/.venv/src/pip-test-package" >, "name": "setuptools", "version": "57.4.0" >, "name": "wheel", "version": "0.36.2" > ]
C:\> py -m pip list --format=json | py -m json.tool [ "name": "pip", "version": "21.2.4", >, "name": "pip-test-package", "version": "0.1.1", "editable_project_location": "C:\Users\You\.venv\src\pip-test-package" >, "name": "setuptools", "version": "57.4.0" >, "name": "wheel", "version": "0.36.2" > ]
Contrary to the freeze command, pip list —format=freeze will not report editable install information, but the version of the package at the time it was installed.
Check available packages python
Last updated: Feb 23, 2023
Reading time · 3 min
# Table of Contents
# Check if a Python package is installed
To check if a Python package is installed:
- Import the package in a try block.
- Use an except block to handle the potential ModuleNotFoundError .
- If the try block runs successfully, the module is installed.
Copied!try: import requests print('The requests module is installed') except ModuleNotFoundError: print('The requests module is NOT installed')
We used a try/except block to check if a module is installed.
If the try block doesn’t raise an exception, the module is installed.
If the module isn’t installed, a ModuleNotFoundError error is raised and the except block runs.
# Installing the module if it isn’t already installed
You can also extend the try/except statement to install the module if it isn’t installed.
Copied!import sys import subprocess try: import requests print('The requests module is installed') except ModuleNotFoundError: print('The requests module is NOT installed') # 👇️ optionally install module python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL ) finally: import requests
If you need to check if a package is installed using pip , use the pip show command.
The pip show module_name command will either state that the package is not installed or show a bunch of information about the package, including the location where the package is installed.
You can also use the following one-liner command.
Copied!python -c 'import pkgutil; print(1 if pkgutil.find_loader("module_name") else 0)'
Make sure to replace module_name with the actual name of the module you are checking for.
The command returns 1 if the module is installed and 0 if it isn’t but this can be easily adjusted.
# Check if a Python package is installed using find_spec
An alternative approach is to use the importlib.util.find_spec method.
The find_spec() method will return a spec object if the module is installed, otherwise, None is returned.
Copied!import importlib.util module_name = 'requests' spec = importlib.util.find_spec(module_name) if spec: print(f'The module_name> module is installed') else: print(f'The module_name> module is NOT installed')
The importlib.util.find_spec method finds the spec for a module.
The spec for a module is a namespace containing the import-related information used to load the module.
If no spec is found, the method returns None.
# Installing the package if it isn’t already installed
You can optionally install the package if it isn’t already installed.
Copied!import importlib.util import sys import subprocess module_name = 'requests' spec = importlib.util.find_spec(module_name) if spec: print(f'The module_name> module is installed') else: print(f'The module_name> module is NOT installed') # 👇️ optionally install the module if it's not installed python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL )
# Checking if the module isn’t installed
If you need to check if the module isn’t installed, check if the spec variable stores a None value.
Copied!import importlib.util import sys import subprocess module_name = 'requests' spec = importlib.util.find_spec(module_name) if spec is None: print(f'The module_name> module is NOT installed') # 👇️ optionally install the module if it's not installed python = sys.executable subprocess.check_call( [python, '-m', 'pip', 'install', 'requests'], stdout=subprocess.DEVNULL ) print(f'The module_name> module is now installed')
Which approach you pick is a matter of personal preference. I’d use the try/except statement because I find it quite direct and easy to read.
# Additional Resources
You can learn more about the related topics by checking out the following tutorials:
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
How to List Installed Python Packages
The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages.
You can also use the ActiveState Platform’s command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command. For a complete list of all packages and dependencies (including OS-level and transitive dependencies, as well as shared libraries), you can use the Web GUI, which provides a full Bill of Materials view. Give it a try by signing up for a free ActiveState Platform account .
Before getting a list of installed packages, it’s always a good practice to ensure that up-to-date versions of Python, Pip, Anaconda Navigator and Conda are in place.
List Installed Packages with Pip
Both pip list and pip freeze will generate a list of installed packages, just with differently formatted results. Keep in mind that pip list will list ALL installed packages (regardless of how they were installed). while pip freeze will list only everything installed by Pip.
Package Version ---------------------------------- ---------- absl-py 0.7.0
List Packages in a Console with Pip
To list all installed packages from a Python console using pip, you can utilize the following script:
>>> import pkg_resources installed_packages = pkg_resources.working_set installed_packages_list = sorted(["%s==%s" % (i.key, i.version) for i in installed_packages]) print(installed_packages_list)
['absl-py==0.7.0', 'adodbapi==2.6.0.7', 'alabaster==0.7.12', 'alembic==1.0.7', 'amqp==2.4.1', 'anyjson==0.3.3',
List Modules in a Console without Pip
To list all installed modules from a python console without pip, you can use the following command:
Note that there are some drawbacks to this approach, including:
- If there are a lot of installed packages, this method can take a long time to import each module before it can search that module’s path for sub-modules.
- Modules that have code outside of an if __name__ == “__main__”: code block, and if user input is expected, may cause the code to enter an infinite loop or hang.
List Installed Packages with Pipenv
The pipenv lock -r command can be used to generate output from a pipfile.lock file in a pipenv environment. All packages, including dependencies will be listed in the output. For example:
-i https://pypi.org/simple certifi==2019.11.28 chardet==3.0.4 idna==2.9 requests==2.23.0 urllib3==1.25.8
List Installed Packages with Anaconda Navigator
To list installed packages in an Anaconda environment using Anaconda Navigator, do the following:
- Start the Anaconda Navigator application.
- Select Environments in the left column.
- A dropdown box at the center-top of the GUI should list installed packages. If not, then select Installed in the dropdown menu to list all packages.
List Installed Packages with Conda
The conda list command can be used to list all packages in a conda environment:
# packages in environment at C:\Anaconda2_4.3.1: # _license 1.1 py27_1 alabaster 0.7.9 py27_0
Globally vs Locally Installed Packages
For information about generating a list of installed packages globally vs locally, refer to:
List Installed Packages with the ActiveState Platform
To view a list of installed Python packages in your currently active project using the ActiveState Platform, run the following command on the command line:
The output is a full list of installed packages in your current project:
matplotlib numpy pandas scikit-learn scipy
You can also obtain a complete software bill of materials view of all packages, dependencies, transitives dependencies (ie., dependencies of dependencies), OS-level dependencies and shared libraries (ie., OpenSSL) using the ActiveState Platform’s Web GUI:
The ActiveState Platform automatically builds all Python packages including linked C libraries from source code, and packages them for Windows, Linux and macOS. Because it does it all server-side, there’s no need to maintain local build environments.