How To Check For Python Dependencies with Popular Package Managers

Python Dependency Check

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 
  <packagename> <version#> requires <depname>, 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:

conda install <package-name>

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:

  1. Install Pipdeptree:
$ pip install pipdeptree 

2. Enter the following command to display a dependency tree for a setuptools project or package:

$ pipdeptree --packages <projectname> 
# Each dependency (including its version#) is verified as installed.
<projectname>==<version#>
  - <depname> [required: <unpinned_version#>, installed: <pinned_version#>]
 - <depname> [required: <unpinned_version#>, installed: <pinned_version#>]

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:

$ conda install pipdeptree 

Enter the following command to display a dependency tree for a project or package:

$ pipdeptree --packages <projectname> 
# Each dependency (including its version#) is verified as installed.
<projectname>==<version#>
- <depname> [required: <unpinned_version#>, installed: <pinned_version#>]
- <depname> [required: <unpinned_version#>, installed: <pinned_version#>]

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)

package management PythonThe 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:

Windows

powershell -Command "& $([scriptblock]::Create((New-Object Net.WebClient).DownloadString('https://platform.activestate.com/dl/cli/install.ps1'))) -activate-default ActiveState-Labs/Python-3.9Beta"

Linux

sh <(curl -q https://platform.activestate.com/dl/cli/install.sh) --activate-default ActiveState-Labs/Python-3.9Beta

Now you can run state install <packagename>. Learn more about how to use the State Tool to manage your Python environment.

Related Links

Recent Posts

Scroll to Top