How to verify that a package installed from a list of package names
<p>I'm writing a program that will install certain packages from a <code>whl</code> file, however I need a way to verify that the packages where installed:</p>

<pre><code>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)
</code></pre>

<p>This will throw the error:</p>

<pre><code>Traceback (most recent call last):
  File "run_setup.py", line 34, in &lt;module&gt;
    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
</code></pre>

<p>If I attempt to to run a loop of the packages and use <code>import</code> like so:</p>

<pre><code>def verify_installs(self):
    for pack in self.packages:
        import pack 
</code></pre>

<p>I'll get the error:</p>

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

<p>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:</p>

<pre><code>def verify_packs(pack_list):
    for pack in pack_list:
        try:
            import pack
        except ImportError:
            print "{} failed to install".format(pack)
</code></pre>

<p>I figured out a way to check the installed packages:</p>

<pre><code>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)
</code></pre>

<p>Example:</p>

<pre><code>test = SetUpProgram(["lxml", "test", "testing"], None, None)
test.verify_installs()
</code></pre>

<p>Output:</p>

<pre><code>Package test was not installed
Package testing was not installed
</code></pre>

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

<pre><code>['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']
</code></pre>

<p>A list of all locally installed packages on the computer, from there:</p>

<pre><code>if pack not in installed_packs:
    print "Package {} was not installed".format(pack)
</code></pre>

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

python python-2.7 loops import