Open In App
Related Articles

How To List Installed Python Packages

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

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:

Python3

import pkg_resources
 
installed_packages = pkg_resources.working_set
for 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

Running this command within a notebook cell will display the list of installed packages and their versions in the notebook output.



Last Updated : 29 Sep, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads