- How to Check for Python Dependencies
- Pip Check Command – Check Python Dependencies After Installation
- Conda Environment Check – Check Python Dependencies at Installation Time
- Poetry Environment Check
- Pipdeptree Verification of Installed Python Dependencies
- Pipdeptree and Setuptools
- Pipdeptree and Virtual Environments
- Pipdeptree and Conda
- ActiveState Platform
- Conclusions: The Future of Python Package & Dependency Management
- Finding Python package dependencies
- Find package dependencies with Pipdeptree
- Pip: Show Python Package Dependencies
- Show Python Package Dependencies
How to Check for Python Dependencies
Sometimes during installation of a dependency, an issue will arise. In some cases, it’s a missing requirement, while at other times you’ll be confronted by a version conflict. Luckily, there are a number of utilities available to you to help at least identify, if not resolve the issue.
Depending on which package manager you use (pip, conda, or Poetry), they all have different approaches to checking for Python dependencies and identifying issues.
Pip Check Command – Check Python Dependencies After Installation
Because pip doesn’t currently address dependency issues on installation, the pip check command option can be used to verify that dependencies have been installed properly in your project.
For example:
$ pip check No broken requirements found.
The response indicates that all Python dependencies in the Python project’s current environment are installed and compatible. However, in the next example, the current environment is missing a dependency:
$ pip check requires , which is not installed.
In this case, you’ll need to manually install the missing dependency.
Conda Environment Check – Check Python Dependencies at Installation Time
Unlike pip, conda checks Python dependencies at installation time and tries to identify conflicts and errors before they happen. For example:
May result in the following message:
Found conflicts! Looking for incompatible packages. This can take several minutes. Press CTRL-C to abort.
Typically, this can result in a list of dependency names and versions that are incompatible with one another that you will need to manually resolve before the package can be installed. See the section below on “Pipdeptree and Conda” for information on how to visualize the conflicts so you can solve them more easily.
Poetry Environment Check
Like conda, Poetry has a solver built into the install command that can help identify dependency conflicts and ensure environments contain a full set of required dependencies.
When you use the poetry install command to install dependencies, it will check for conflicts and then create a poetry.lock file that contains a list of all your project’s Python dependencies.
Similarly, when updating your project, the poetry update command resolves all project dependencies for you, and writes the exact versions into the poetry.lock file:
(poetryproject) $ poetry update Updating dependencies Resolving dependencies. Writing lock file Package operations: 0 installs, 3 updates, 0 removals - Updating atomicwrites (1.3.0 -> 1.4.0) - Updating more-itertools (8.2.0 -> 8.3.0) - Updating pytest (5.4.1 -> 5.4.2)
Pipdeptree Verification of Installed Python Dependencies
When conflicts do occur, it’s usually far easier to visualize dependency tree conflicts than try to muddle through them on the command line. That’s where pipdeptree comes in. It’s a useful utility for displaying installed packages and dependencies in the form of a visual tree that is easy to understand at a glance. Pipdeptree can be used to verify whether all dependencies for a project have been installed correctly.
Pipdeptree and Setuptools
For example, to install Pipdeptree and display a dependency tree for a setuptools project:
2. Enter the following command to display a dependency tree for a setuptools project or package:
$ pipdeptree --packages # Each dependency (including its version#) is verified as installed. == - [required: , installed: ] - [required: , installed: ]
You can optimally just compare the project dependency tree to the minimum dependency requirements stated in the install_requires section of the project setup.py file.
For more information about setup.py and install_requires , refer to: How to Package Python Dependencies for Publication
Pipdeptree and Virtual Environments
To install pipdeptree and display a dependency tree for a virtual environment, pipdeptree needs to be installed in the same virtual environment that it is to be used in. To install pipdeptree in a pipenv environment, cd into the environment and enter:
(pipenv_env) $ pipenv install pipdeptree
For more information about using pipdeptree in a virtual environment, refer to: Python Dependency Management Tools
Pipdeptree and Conda
To install pipdeptree and display a dependency tree for a Conda project, enter:
Enter the following command to display a dependency tree for a project or package:
$ pipdeptree --packages # Each dependency (including its version#) is verified as installed. == - [required: , installed: ] - [required: , installed: ]
ActiveState Platform
The ActiveState Platform is a cloud-based build automation and dependency management tool for Python. It provides dependency resolution for:
- Python language cores, including Python 2.7 and Python 3.5+
- Python packages and their dependencies, including:
- Transitive dependencies (ie., dependencies of dependencies)
- Linked C and Fortran libraries, so you can build data science packages
- Operating system-level dependencies for Windows, Linux, and macOS
- Shared dependencies (ie., OpenSSL)
The ActiveState Platform is the only Python package management solution that not only resolves dependencies but also provides workarounds for dependency conflicts.
Simply following the instruction prompts will resolve the conflict, eliminating dependency hell.
You can try the ActiveState Platform for free by creating an account using your email or your GitHub credentials. Start by creating a new Python project, pick the latest version that applies to your project, your OS and start to add & install packages. Or start by simply importing your requirements.txt file and creating a Python version with all the Python libraries you need.
Conclusions: The Future of Python Package & Dependency Management
If you’re like most Python developers, you probably maintain multiple tools, build environments, and other solutions in order to address the issues that a single tool, the ActiveState Platform can solve today. By adopting the ActiveState Platform, you can:
- Increase the security of your Python environments
- Improve the transparency of your open source supply chain
- Eliminate dependency hell
- Reduce “works on my machine” issues
Ultimately, developers that are willing to adopt the ActiveState Platform for Python packaging will spend less time wrestling with tooling and more time focused on doing what they do best: coding.
Just run the following command to install Python 3.9 and our package manager, the State Tool:
powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1'))) -activate-default ActiveState-Labs/Python-3.9Beta"
Now you can run state install . Learn more about how to use the State Tool to manage your Python environment.
Finding Python package dependencies
To find a Python package’s dependencies, you can parse a package’s PyPi JSON endpoint. These endpoints can be found at:
You can parse the JSON at this URL with the requests package, as follows:
package_json = requests.get('https://pypi.org/pypi/seaborn/0.11.2/json', verify = False).json() package_json['info']['requires_dist']
If you run this for the Seaborn package, you’ll get: [‘numpy (>=1.15)’, ‘scipy (>=1.0)’, ‘pandas (>=0.23)’, ‘matplotlib (>=2.2)’]
But there’s an issue here. Some of Seaborn’s dependencies have dependencies themselves. They aren’t listed here. Luckily, there is a solution. Read on!
Find package dependencies with Pipdeptree
If you want to find all (including nested) dependencies: use pipdeptree, which is a command-line tool with many options for listing and visualizing dependencies.
pip install pipdeptree pipdeptree
Runnin the pipdeptree command in your terminal will print all of your packages’ dependencies. In the example below, I took a snapshot from my pipdeptree output and show what Seaborn’s dependencies look like.
seaborn==0.11.2 - matplotlib [required: >=2.2, installed: 3.5.1] - cycler [required: >=0.10, installed: 0.11.0] - fonttools [required: >=4.22.0, installed: 4.31.2] - kiwisolver [required: >=1.0.1, installed: 1.4.1] - numpy [required: >=1.17, installed: 1.22.3] - packaging [required: >=20.0, installed: 21.3] - pyparsing [required: >=2.0.2,!=3.0.5, installed: 3.0.7] - pillow [required: >=6.2.0, installed: 9.0.1] - pyparsing [required: >=2.2.1, installed: 3.0.7] - python-dateutil [required: >=2.7, installed: 2.8.2] - six [required: >=1.5, installed: 1.16.0] - numpy [required: >=1.15, installed: 1.22.3] - pandas [required: >=0.23, installed: 1.4.1] - numpy [required: >=1.21.0, installed: 1.22.3] - python-dateutil [required: >=2.8.1, installed: 2.8.2] - six [required: >=1.5, installed: 1.16.0] - pytz [required: >=2020.1, installed: 2022.1] - scipy [required: >=1.0, installed: 1.8.0] - numpy [required: >=1.17.3,
If you want to get pipdeptree’s output in a text file, use:
pipdeptree >> dependencies.txt
Congratulations, you can now find the dependencies of Python packages.
Pip: Show Python Package Dependencies
The dependencies of the installed Python packages can be listed using the built-in pip show command.
Alternatively the dependencies can be shown as a tree structure using the pipdeptree command.
In this note i will show several examples of how to list dependencies of the installed Python packages.
Cool Tip: How to install specific version of a package using pip ! Read More →
Show Python Package Dependencies
Use the built-in pip show command to list dependencies of the Python packages that has already been installed, e.g.:
$ pip show Flask Name: Flask Version: 1.1.2 . Requires: click, Werkzeug, itsdangerous, Jinja2
Show requirements of the all installed Python packages:
$ pip freeze | cut -d "=" -f1 |\ xargs pip show |\ grep -i "^name\|^version\|^requires"
Cool Tip: How to list all the locally installed Python modules and find the paths to their source files! Read More →
If you don’t mind installing new packages, you can install the pipdeptree that displays information about the dependencies as a tree structure:
$ pip install pipdeptree $ pipdeptree skywriter==0.0.7 - RPi.GPIO [required: Any, installed: 0.7.0] touchphat==0.0.1 - cap1xxx [required: Any, installed: 0.1.3] - RPi.GPIO [required: Any, installed: 0.7.0] .
It also can show the dependency tree in the reverse fashion, i.e. list the sub-dependencies with the list of Python packages that require them:
$ pipdeptree -r -p RPi.GPIO RPi.GPIO==0.7.0 - automationhat==0.2.0 [requires: RPi.GPIO] - blinkt==0.1.2 [requires: RPi.GPIO] - Cap1xxx==0.1.3 [requires: RPi.GPIO] - drumhat==0.1.0 [requires: cap1xxx] .