Pip: Command to check install against requirements.txt

Created on 28 Apr 2015  路  8Comments  路  Source: pypa/pip

It would be very useful to have a pip command to simply check if the current environment meets the stated requirements of a requirements.txt file, without actually downloading or installing anything.

auto-locked

Most helpful comment

I came up with something like this that might help.

import pip
from pip.req import parse_requirements
import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict

def check_dependencies(requirement_file_name):
    """
    Checks to see if the python dependencies are fullfilled.
    If check passes return 0. Otherwise print error and return 1
    """
    dependencies = []
    session = pip.download.PipSession()
    for req in parse_requirements(requirement_file_name, session=session):
        if req.req is not None:
            dependencies.append(str(req.req))
        else:
            # The req probably refers to a url. Depending on the
            # format req.link.url may be able to be parsed to find the
            # required package.
            pass
    try:
        pkg_resources.working_set.require(dependencies)
    except VersionConflict as e:
        try:
            print("{} was found on your system, "
                  "but {} is required.\n".format(e.dist, e.req))
            return 1
        except AttributeError:
            print(e)
            return 1
    except DistributionNotFound as e:
        print(e)
        return 1
    return 0

All 8 comments

I was also thinking of this to point as the issue

This would be very useful, especially when used with constraints files, to determine whether the requirements can be matched given the specified constraints.

:+1:

Have any of you solved this issue privately?

Duplicate of issue #716?

Similar, but the use case is different.

In this case, knowing which requirements needs installing/updating allows the user to utilise pip to understand the requirements that are not met, and then use that information in different ways.
e.g. I like to try installing missing dependencies using my OS package manager if possible, and there are some people who will build their own OS packages if they are missing.

Some people may have no rights to install the dependencies on a box, and need to report the list to someone else to install into the tightly controlled box , or even add them to the SOE.

I came up with something like this that might help.

import pip
from pip.req import parse_requirements
import pkg_resources
from pkg_resources import DistributionNotFound, VersionConflict

def check_dependencies(requirement_file_name):
    """
    Checks to see if the python dependencies are fullfilled.
    If check passes return 0. Otherwise print error and return 1
    """
    dependencies = []
    session = pip.download.PipSession()
    for req in parse_requirements(requirement_file_name, session=session):
        if req.req is not None:
            dependencies.append(str(req.req))
        else:
            # The req probably refers to a url. Depending on the
            # format req.link.url may be able to be parsed to find the
            # required package.
            pass
    try:
        pkg_resources.working_set.require(dependencies)
    except VersionConflict as e:
        try:
            print("{} was found on your system, "
                  "but {} is required.\n".format(e.dist, e.req))
            return 1
        except AttributeError:
            print(e)
            return 1
    except DistributionNotFound as e:
        print(e)
        return 1
    return 0

I'm going to close this. While I recognize that there are folks who want this, I think that adding this doesn't make sense to pip itself.

Was this page helpful?
0 / 5 - 0 ratings