6

I'd like to programmatically run pip and determine whether the current virtualenv environment complies with a specified requirements.txt file. I'm not fussed about running pip or anything, but I thought since it can read requirements.txt-like files, it would be a good start.

However, I haven't even found a way of effectively running pip from the command line. pip install -r requirements.txt --no-install was suggested somewhere, but it downloads each package and even if this wasn't a problem, I am unsure of how to interpret its output as to whether or not all dependencies are satisfied.

orange
  • 7,755
  • 14
  • 75
  • 139
  • See this relevant thread: http://stackoverflow.com/questions/16294819/how-to-check-if-my-python-has-all-required-packages – alecxe Mar 06 '14 at 03:27
  • Thanks. I didn't see this thread. Only the response `pkg_resources` seems to be useful, but I doubt that it'll work with things like `github` repositories as dependency in `requirements.txt`. I might need to resort to this approach, if this is the only option... – orange Mar 06 '14 at 07:11
  • 1
    Possible duplicate of [How to check if my Python has all required packages?](https://stackoverflow.com/questions/16294819/how-to-check-if-my-python-has-all-required-packages) – Asclepius Aug 03 '17 at 03:52

1 Answers1

1

This post has a lot of good suggestions for getting a list of modules. You can use the below code to print out all missing modules:

from pkgutil import iter_modules
modules = set(x[1] for x in iter_modules())

with open('requirements.txt', 'rb') as f:
    for line in f:
        requirement = line.rstrip()
        if not requirement in modules:
            print requirement
Community
  • 1
  • 1
James Mnatzaganian
  • 1,255
  • 1
  • 17
  • 32