How to Check for Python Dependencies

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 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#>]

Recommended Reads:

The State of Package Management (and How to Make It Better)

How to Package Python Dependencies for Publication

How to use Python Dependency Management Tools

 

Remi M