In order to add to my program (in python 2.7) a check for available modules, I added the following code in place of the classical import (the idea being to help someone to locate & add extra modules):
mymodules = ['socket', 'requests', 'simplejson', 'pickle', 'IPy',
'pygeoip', 'urllib', 'time', 'urllib2', 'StringIO', 'gzip', 'os']
import sys, importlib # these ones should be available, otherwise bad luck :)
for module in mymodules:
try:
importlib.import_module(module)
print "importing ", module
except:
if module == "requests": info = "http://docs.python-requests.org/en/latest/user/install/#install or aptitude install python-requests"
elif module == "requests": info = "https://github.com/simplejson/simplejson or aptitude install python-simplejson"
elif module == "IPy": info = "https://github.com/haypo/python-ipy/wiki or aptitude install python-ipy"
elif module == "pygeoip": info = "https://github.com/appliedsec/pygeoip or pip install pygeoip"
else: info = "Oops, you should not see this - the description of the missing plugin is missing in the code"
print "module {} is missing, see {}".format(module,info)
sys.exit(0)
Later on my program crashes with NameError
on a call to time.time()
('time' is not defined). I therefore tried to test module importing from scratch:
>>> import sys, importlib
>>> importlib.import_module("time")
<module 'time' (built-in)>
>>> print sys.modules.keys()
['copy_reg', 'sre_compile', '_sre', 'encodings', 'site', '__builtin__', 'sysconfig', '__main__', 'encodings.encodings', 'abc', 'importlib.sys', 'posixpath', '_weakrefset', 'errno', 'encodings.codecs', 'sre_constants', 're', '_abcoll', 'types', '_codecs', 'encodings.__builtin__', '_warnings', 'genericpath', 'stat', 'zipimport', '_sysconfigdata', 'warnings', 'UserDict', 'encodings.utf_8', 'sys', 'codecs', 'readline', '_sysconfigdata_nd', 'os.path', 'importlib', 'sitecustomize', 'signal', 'traceback', 'linecache', 'posix', 'encodings.aliases', 'time', 'exceptions', 'sre_parse', 'os', '_weakref']
time
is there. Nevertheless:
>>> print time.time()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'time' is not defined
Now with a classical import:
>>> import time
>>> print time.time()
1380831191.08
Why doesn't importlib.import_module("time")
imports time
in such a way that time.time()
can be called?
From the docs:
The specified module will be inserted into
sys.modules
and returned.
In other words, import_module
will not create a variable for you, you will have to do it yourself:
time = importlib.import_module('time')
Or, in your "dynamic" case:
globals()['time'] = importlib.import_module('time')
On a side note, why do you even do this? Why not just wrap a normal import
in a try
-except
block?
I think importlib.import_module("time")
returns an object, we need to assign it to a variable. Use that variable to invoke all the methods of time.
Try:
import sys, importlib
var_name = importlib.import_module("time")
print var_name.time()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With