-1

I'm writing a program that will install certain packages from a whl file, however I need a way to verify that the packages where installed:

def verify_installs(self):
    for pack in self.packages:
        import pip

        installed = pip.get_installed_distributions()

        for name in list(installed):
            if pack not in name:
                print "{} failed to install.".format(pack)

This will throw the error:

Traceback (most recent call last):
  File "run_setup.py", line 34, in <module>
    test.verify_installs()
  File "run_setup.py", line 29, in verify_installs
    if pack not in name:
TypeError: argument of type 'Distribution' is not iterable

If I attempt to to run a loop of the packages and use import like so:

def verify_installs(self):
    for pack in self.packages:
        import pack 

I'll get the error:

Traceback (most recent call last):
  File "run_setup.py", line 29, in <module>
    test.verify_installs()
  File "run_setup.py", line 24, in verify_installs
    import pack
ImportError: No module named pack

Is there a way I can loop through a list of packages and then try to import them and catch the import exception? Something like:

def verify_packs(pack_list):
    for pack in pack_list:
        try:
            import pack
        except ImportError:
            print "{} failed to install".format(pack)
  • What were you expecting `if pack not in name` to do? What did you think it would mean for `pack` to be `in name`? – user2357112 Sep 07 '16 at 21:05
  • @user2357112 Well the installed variable throws a string of installed packages, so I'd expect it to see if the pack in the list is inside of that string that's output – YoYoYo I'm Awesome Sep 07 '16 at 21:06
  • 1
    You have misunderstood what's going on and how Python works in a lot of weird ways. – user2357112 Sep 07 '16 at 21:09
  • @user2357112 I kinda figured I did, is there anyway to fix it? – YoYoYo I'm Awesome Sep 07 '16 at 21:12
  • I'm not sufficiently familiar with pip to say what the right way would be, but I can explain some of what's going wrong here. The `installed` variable doesn't throw anything - the word "throw" doesn't even make sense there - and it's not a string of installed packages. The word "string" doesn't make much sense there either. The `installed` variable holds a list of Distribution objects representing installed distributions. Calling `list` on this list is redundant, and trying to test whether something is `in` one of the contained `Distribution` objects doesn't make sense. – user2357112 Sep 07 '16 at 21:27

2 Answers2

0

Say pack_list is a list of string names of modules:

import importlib

def verify_packs(pack_list):
    for pack in pack_list:
        try:
            importlib.import_module(pack)
        except ImportError:
            print("{} failed to install".format(pack))

Note that this is not the preferable way to check if a module was installed and available.
Take a look here.

Community
  • 1
  • 1
galah92
  • 3,621
  • 2
  • 29
  • 55
0

I figured out a way to check the installed packages:

def verify_installs(self):
    for pack in self.packages:
        import pip
        items = pip.get_installed_distributions()
        installed_packs = sorted(["{}".format(i.key) for i in items])
        if pack not in installed_packs:
            print "Package {} was not installed".format(pack)

Example:

test = SetUpProgram(["lxml", "test", "testing"], None, None)
test.verify_installs()

Output:

Package test was not installed
Package testing was not installed

Now to explain it, this part installed_packs = sorted(["{}".format(i.key) for i in items]) will create this:

['babelfish', 'backports.shutil-get-terminal-size', 'beautifulsoup', 'chardet',
'cmd2', 'colorama', 'cycler', 'decorator', 'django', 'easyprocess', 'gooey', 'gu
essit', 'hachoir-core', 'hachoir-metadata', 'hachoir-parser', 'ipython', 'ipytho
n-genutils', 'lxml', 'matplotlib', 'mechanize', 'mypackage', 'nose', 'numpy', 'p
athlib2', 'pickleshare', 'pillow', 'pip', 'prettytable', 'progressbar', 'prompt-
toolkit', 'pygments', 'pyinstaller', 'pyparsing', 'python-dateutil', 'python-geo
ip', 'python-geoip-geolite2', 'pytz', 'pyvirtualdisplay', 'rebulk', 'requests',
'scapy', 'scrappy', 'selenium', 'setuptools', 'simplegeneric', 'six', 'tinydb',
'traitlets', 'tvdb-api', 'twisted', 'wcwidth', 'win-unicode-console', 'zope.inte
rface']

A list of all locally installed packages on the computer, from there:

if pack not in installed_packs:
    print "Package {} was not installed".format(pack)

Will run through the packages and check if any of the packages in a given list correspond to any of the actual install packages.