How to find the dependencies of a Python package

avatar
Borislav Hadzhiev

Last updated: Apr 11, 2024
4 min

banner

# Table of Contents

  1. How to find the dependencies of a Python package
  2. Find the dependencies of a Python package, including the version
  3. Displaying a dependency tree of the installed Python packages
  4. Displaying a dependency tree of a package using the johnnydep module
  5. Find a package's dependencies with an HTTP request
  6. Generating a pip requirements.txt file based on your imports

# How to find the dependencies of a Python package

You can use the pip show command to find the dependencies of a Python package, e.g. pip show requests.

The pip show command will list the dependencies of the specified package under the Requires key.

shell
pip show <YOUR_PACKAGE> # Or with pip3 pip3 show <YOUR_PACKAGE>

Make sure to replace the <YOUR_PACKAGE> placeholder with the name of the actual package.

find python package dependencies using pip show

The pip show command shows information about one or more installed packages.

As shown in the screenshot, the Requires key lists the dependencies of the package separated by commas.

If the pip show <YOUR_PACKAGE> command doesn't work, try to run it with pip3.

shell
pip3 show <YOUR_PACKAGE>

You can also use the python -m prefix.

shell
python -m pip show requests # Or python3 python3 -m pip show requests

find dependencies of package using python m

If you use conda, you can also use the conda search command.

shell
conda search requests --info

find python package dependencies using conda

Make sure to replace requests with the name of your specific package.

By default, the command lists the dependencies of all versions of the package and all channels.

You can also specify a version when running the command.

shell
conda search "requests==2.29.0" --info

Or specify a version and a channel.

shell
conda search "requests==2.29.0" --info --channel "anaconda"

# Find the dependencies of a Python package, including the version

If you need to find a package's dependencies, including the version, use the pkg_resources module.

main.py
from pip._vendor import pkg_resources def find_dependencies(package_name): package = pkg_resources.working_set.by_key[package_name] print([str(dependency) for dependency in package.requires()]) find_dependencies('requests')

print package dependencies including version

The code sample prints the dependencies of the requests package but you can call the function with any other package.

Running the python main.py command produces the following output.

output
['idna<4,>=2.5', 'urllib3<1.27,>=1.21.1', 'certifi>=2017.4.17', 'charset-normalizer<4,>=2']

The script prints a list containing the dependencies of the specified package, including the version of each dependency.

# Displaying a dependency tree of the installed Python packages

If you need to display a dependency tree of the installed Python packages, use the pipdeptree module.

Open your terminal and install the module.

shell
pip install pipdeptree # Or with pip3 pip3 install pipdeptree

pip install pipdeptree

Now run the pipdeptree command.

shell
pipdeptree

run pipdeptree command

The command will display your installed Python packages in a tree form.

You can also use the --json option if you want to format the output as JSON.

shell
pipdeptree --json

print package dependencies in json format

If you only need to print top-level dependencies, use the following command instead.

shell
pipdeptree | grep -P '^\w+'

print only top level dependencies

The command assumes that you have the grep package installed.

You can also silence any warnings in the output.

shell
pipdeptree --warn silence | grep -E '^\w+'

You can also use the pipdeptree command to find out why a particular package is installed.

shell
pipdeptree --reverse --packages itsdangerous,MarkupSafe

find out why a particular package is installed

As shown in the screenshot, the MarkupSafe package is installed because it is required by Jinja2.

You can view more examples of using pipdeptree in the package's GitHub page.

# Displaying a dependency tree of a package using the johnnydep module

You can also use the johnnydep module to display a dependency tree of a Python package.

First, install the module by running the following command.

shell
pip install johnnydep pip3 install johnnydep

install johnnydep package

Now use the johnnydep <YOUR_PACKAGE> command.

shell
johnnydep requests

display dependencies of packing using johnnydep module

The module's dependencies are displayed at the bottom of the output.

The johnnydep module can also be used to resolve the dependency tree of the package.

shell
johnnydep requests --output-format pinned

resolve dependency tree of package

You can view more examples on the package's GitHub page.

# Find a package's dependencies with an HTTP request

You can also find a package's dependencies by making an HTTP request to the package's Pypi page.

First, make sure you have the requests module installed.

shell
pip install requests # Or with pip3 pip3 install requests

Now import and use the module as follows.

main.py
import requests def find_package_dependencies(package_name): url = 'https://pypi.org/pypi/{}/json' json = requests.get(url.format(package_name), timeout=10).json() print(json['info']['requires_dist']) print('-' * 50) print('Required Python version:', json['info']['requires_python']) find_package_dependencies('requests')

Running the python main.py command produces the following output.

find package dependencies with http request

The example lists the dependencies of the requests package but you can call the function with any other package name.

# Generating a pip requirements.txt file based on your imports

If you need to generate a pip requirements.txt file based on the imports in your project, use the pipreqs module.

First, install the module by running the following command.

shell
pip install pipreqs # Or with pip3 pip3 install pipreqs

Now you can use the module by passing it the path to your project's root directory.

shell
pipreqs /path/to/your/project

The package will generate a requirements.txt file that is formatted as follows.

requirements.txt
wheel==0.23.0 Yarg==0.1.9 docopt==0.6.2

The pip freeze command can also be used to generate a requirements.txt file.

shell
pip freeze > requirements.txt # Or with pip3 pip3 freeze > requirements.txt

However, the pip freeze command only saves the packages that are installed with pip install in your environment.

# 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.
book cover
You can use the search field on my Home Page to filter through all of my articles.