Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if my Python has all required packages

I have a requirements.txt file with a list of packages that are required for my virtual environment. Is it possible to find out whether all the packages mentioned in the file are present. If some packages are missing, how to find out which are the missing packages?

like image 654
Alagappan Ramu Avatar asked Apr 30 '13 07:04

Alagappan Ramu


People also ask

How do you check Python requirements?

If you are using flake8 for style-checking, you can add flake8-requirements plugin for flake8. It will automatically check whether imported modules are available in setup.py , requirements. txt or pyproject. toml file.

How do you check what all Python packages are installed?

The Pip, Pipenv, Anaconda Navigator, and Conda Package Managers can all be used to list installed Python packages. You can also use the ActiveState Platform's command line interface (CLI), the State Tool to list all installed packages using a simple “state packages” command.

How do I find the dependencies of a Python package?

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.


5 Answers

UPDATE:

An up-to-date and improved way to do this is via distutils.text_file.TextFile. See Acumenus' answer below for details.

ORIGINAL:

The pythonic way of doing it is via the pkg_resources API. The requirements are written in a format understood by setuptools. E.g:

Werkzeug>=0.6.1
Flask
Django>=1.3

The example code:

import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict

# dependencies can be any iterable with strings, 
# e.g. file line-by-line iterator
dependencies = [
  'Werkzeug>=0.6.1',
  'Flask>=0.9',
]

# here, if a dependency is not met, a DistributionNotFound or VersionConflict
# exception is thrown. 
pkg_resources.require(dependencies)
like image 119
Zaur Nasibov Avatar answered Oct 27 '22 04:10

Zaur Nasibov


Based on the answer by Zaur, assuming you indeed use a requirements file, you may want a unit test, perhaps in tests/test_requirements.py, that confirms the availability of packages.

Moreover, this approach uses a subtest to independently confirm each requirement. This is useful so that all failures are documented. Without subtests, only a single failure is documented.

"""Test availability of required packages."""

import unittest
from pathlib import Path

import pkg_resources

_REQUIREMENTS_PATH = Path(__file__).parent.with_name("requirements.txt")


class TestRequirements(unittest.TestCase):
    """Test availability of required packages."""

    def test_requirements(self):
        """Test that each required package is available."""
        # Ref: https://stackoverflow.com/a/45474387/
        requirements = pkg_resources.parse_requirements(_REQUIREMENTS_PATH.open())
        for requirement in requirements:
            requirement = str(requirement)
            with self.subTest(requirement=requirement):
                pkg_resources.require(requirement)
like image 23
Asclepius Avatar answered Oct 27 '22 04:10

Asclepius


You can run pip freeze to see what you have installed and compare it to your requirements.txt file.

If you want to install missing modules you can run pip install -r requirements.txt and that will install any missing modules and tell you at the end which ones were missing and installed.

like image 20
John Jiang Avatar answered Oct 27 '22 05:10

John Jiang


Here's a one-liner based on Zaur Nasibov's answer for if you don't care to know which packages are not installed:

python3 -c "import pkg_resources; pkg_resources.require(open('requirements.txt',mode='r'))" &>/dev/null

Whether the command finishes successfully can then be used to do a pip install.

As an equivalent to Ruby's bundle check || bundle install, we're doing:

python3 -c "import pkg_resources; pkg_resources.require(open('requirements.txt',mode='r'))" &>/dev/null || pip3 install --ignore-installed -r requirements.txt

I realise this is not addressing the exact question, but this page comes up first when Googling for that. And anyway, you'd only really be wanting to know what the missing dependencies are in order to then satisfy them.

We can't just use pip3 check for this, since it does not look at the requirements.txt

like image 4
ZimbiX Avatar answered Oct 27 '22 04:10

ZimbiX


If you're interested in doing this from the command line, pip-missing-reqs will list missing packages. Example:

$ pip-missing-reqs directory
Missing requirements:
directory/exceptions.py:11 dist=grpcio module=grpc

(pip check and pipdeptree --warn fail only audit installed packages for compatibility with each other, without checking requirements.txt.)

like image 3
cov Avatar answered Oct 27 '22 06:10

cov