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)