Hi,
Not sure why you want to try and conditionally import the sys module. It will always exist never going to fail. Anyway your problem just appears to be a fundamental misunderstanding of between what a variable is and the value that is assigned to the variable…
The import functions return an instance of the module object, this instance not automatically bound to a name in the global namespace like is done when using the normal import keyword statement.
Also unlike the “import” statement ( import foo) the import function takes a string as an argument not an unbound variable ( import_module(“moduleName”) vs import moduleName ) the later keyword statement has special hook that calls the import function and then assigns the loaded object to the named variable in the global namespace.
You, probably would have been able to catch this mistake in your earlier attempts if you had actually looked at the message in the exception that was generated as it would have have said something an failed to import “module” where “module” was literally the name of the module it tried to import and not the value of the string variable you had passed in.
Output from interactive session:
-
First note that import_module returns the module as a local variable (I assigned the “sys” module to variable ‘x’)
-
dir shows that it is the contents of the sys module
-
Then assign x to “sys” in globals, there will now be a global “sys” variable that references the sys module
-
dir(sys) now shows that the global “sys” is the sys module
Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win
32
Type “help”, “copyright”, “credits” or “license” for more information.
import importlib
x = importlib.import_module(“sys”)
dir(x)
[‘displayhook’, ‘doc’, ‘excepthook’, ‘name’, ‘package’, '__s
tderr__', ‘stdin’, ‘stdout’, ‘_clear_type_cache’, ‘_current_frames’, '_g
etframe’, ‘_mercurial’, ‘api_version’, ‘argv’, ‘builtin_module_names’, 'byteorde
r’, ‘call_tracing’, ‘callstats’, ‘copyright’, ‘displayhook’, ‘dllhandle’, 'dont_
write_bytecode’, ‘exc_clear’, ‘exc_info’, ‘exc_type’, ‘excepthook’, 'exec_prefix
', ‘executable’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, 'getcheckint
erval’, ‘getdefaultencoding’, ‘getfilesystemencoding’, ‘getprofile’, 'getrecursi
onlimit’, ‘getrefcount’, ‘getsizeof’, ‘gettrace’, ‘getwindowsversion’, 'hexversi
on’, ‘long_info’, ‘maxint’, ‘maxsize’, ‘maxunicode’, ‘meta_path’, ‘modules’, 'pa
th’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’, ‘ps1’, ‘ps2’, 'p
y3kwarning’, ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’, ‘settrace’,
‘stderr’, ‘stdin’, ‘stdout’, ‘subversion’, ‘version’, ‘version_info’, 'warnoptio
ns’, ‘winver’]
globals()[“sys”] = x
dir(sys)
[‘displayhook’, ‘doc’, ‘excepthook’, ‘name’, ‘package’, '__s
tderr__', ‘stdin’, ‘stdout’, ‘_clear_type_cache’, ‘_current_frames’, '_g
etframe’, ‘_mercurial’, ‘api_version’, ‘argv’, ‘builtin_module_names’, 'byteorde
r’, ‘call_tracing’, ‘callstats’, ‘copyright’, ‘displayhook’, ‘dllhandle’, 'dont_
write_bytecode’, ‘exc_clear’, ‘exc_info’, ‘exc_type’, ‘excepthook’, 'exec_prefix
', ‘executable’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, 'getcheckint
erval’, ‘getdefaultencoding’, ‘getfilesystemencoding’, ‘getprofile’, 'getrecursi
onlimit’, ‘getrefcount’, ‘getsizeof’, ‘gettrace’, ‘getwindowsversion’, 'hexversi
on’, ‘long_info’, ‘maxint’, ‘maxsize’, ‘maxunicode’, ‘meta_path’, ‘modules’, 'pa
th’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’, ‘ps1’, ‘ps2’, 'p
y3kwarning’, ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’, ‘settrace’,
‘stderr’, ‘stdin’, ‘stdout’, ‘subversion’, ‘version’, ‘version_info’, 'warnoptio
ns’, ‘winver’]
print sys.version
2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)]
Cody