You can use __import__
to dynamically import modules, allowing you to - among other things - import modules by iterating a list with their names.
For example:
libnames = ['numpy', 'scipy', 'operator']
for libname in libnames:
try:
lib = __import__(libname)
except:
print sys.exc_info()
else:
globals()[libname] = lib
You can either extend that to handle the import ... as ...
and from ... import ...
forms or just do the assignments later manually, ie.:
np = numpy
sp = scipy
itemgetter = operator.itemgetter