Last updated: Apr 11, 2024
Reading time·4 min
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.
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.
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
.
pip3 show <YOUR_PACKAGE>
You can also use the python -m
prefix.
python -m pip show requests # Or python3 python3 -m pip show requests
If you use conda
, you can also use the conda search
command.
conda search requests --info
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.
conda search "requests==2.29.0" --info
Or specify a version and a channel.
conda search "requests==2.29.0" --info --channel "anaconda"
If you need to find a package's dependencies, including the version, use the
pkg_resources
module.
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')
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.
['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.
If you need to display a dependency tree of the installed Python packages, use the pipdeptree module.
Open your terminal and install the module.
pip install pipdeptree # Or with pip3 pip3 install pipdeptree
Now run the pipdeptree
command.
pipdeptree
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.
pipdeptree --json
If you only need to print top-level dependencies, use the following command instead.
pipdeptree | grep -P '^\w+'
The command assumes that you have the grep
package installed.
You can also silence any warnings in the output.
pipdeptree --warn silence | grep -E '^\w+'
You can also use the pipdeptree
command to find out why a particular package
is installed.
pipdeptree --reverse --packages itsdangerous,MarkupSafe
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.
johnnydep
moduleYou can also use the johnnydep module to display a dependency tree of a Python package.
First, install the module by running the following command.
pip install johnnydep pip3 install johnnydep
Now use the johnnydep <YOUR_PACKAGE>
command.
johnnydep requests
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.
johnnydep requests --output-format pinned
You can view more examples on the package's GitHub page.
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.
pip install requests # Or with pip3 pip3 install requests
Now import and use the module as follows.
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.
The example lists the dependencies of the requests
package but you can call
the function with any other package name.
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.
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.
pipreqs /path/to/your/project
The package will generate a requirements.txt
file that is formatted as
follows.
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.
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.
You can learn more about the related topics by checking out the following tutorials: