import(module) cannot be used as a drop-in replacement for import module, because it returns the imported module rather than initializing it as a variable. This link may be helpful: http://effbot.org/zone/import-string.htm

There are several alternatives to using the import statement or the import() function:

  1. You can use exec(), which is probably undesirable.
  2. You can use the imp module.
  3. You can use the importlib module (as Werner suggested).
    These links may also be helpful:
···

On Tuesday, July 21, 2015 at 1:54:22 PM UTC-4, Boštjan Mejak wrote:

def tryToImport(module):

try:

import module

except ImportError:

print(“The import failed.”)

As you can see from my code above, the function tryToImport() takes a ‘module’ argument which is then taken as a module to be imported. But I always get “The import failed.” message. Why does my code not work? No module can be imported this way, even the built-in Python modules. How can I refactor my code to make this work?

I understand your problem. All of the Python functions that import modules (import, importlib.import_module, and imp.load_source) will only return the module as an object. They will NOT add it to the global namespace. Try using this code for your function (obtained from this StackOverflow answer):

def tryToImport(module):
try:
globals()[module] = import(module)
except:
print(“The import failed.”)

···

On Wednesday, July 22, 2015 at 6:22:41 AM UTC-4, Boštjan Mejak wrote:

def tryToImport(module):

try:

import importlib

importlib.import_module(module)

except ImportError:

print(“The import failed.”)

Try to create the exact same function, as is above, in your interpreter and then do this:

tryToImport(“wx”)

You will notice that everything went smoothly, until you do this:

wx

Now what the hell is this? A NameError I get? But I just imported wx with no errors. Well, my friend, that’s my hell that I am going through.

Days? Seriously? Consider this:
That fails in exactly the same way for exactly the same reason. Can
you see why?

···

Boštjan Mejak wrote:

Your code doesn’t work. Try importing sys
using your code in the interactive interpreter and then type sys and
hit Enter. You’ll get a NameError as if sys wasn’t
imported at all.

I’m struggling with this for days now.

` def one():``

``        x = 3``

``    ``

``    ``one()``

``    print( x )``

`
-- Tim Roberts, Providenza & Boekelheide, Inc.

timr@probo.com

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

Hi,

Globals are per module. if your importing an external module but using its local ‘globals’ the imported module will only exist there.

Modify to pas the globals from the calling context

def tryToImport(module, g):

m = import_module(module)

g[module] = m

···

On Wed, Jul 22, 2015 at 3:42 PM, Boštjan Mejak bostjan.xperia@gmail.com wrote:

There is no god. I just can’t make my function to work. I am giving you the file named importer.py in the attachment for you to look at and modify at will. Import the module importer in the ineractive interpreter and do importer.tryToImport(“sys”) and then after this type in sys and hit Enter. You’ll get a NameError.

You received this message because you are subscribed to the Google Groups “wxPython-users” group.

To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

I tested my function before posting it. The screenshot shows that it works for me. What version of Python are you using?

···

On Wednesday, July 22, 2015 at 1:55:59 PM UTC-4, Boštjan Mejak wrote:

Your code doesn’t work. Try importing sys using your code in the interactive interpreter and then type sys and hit Enter. You’ll get a NameError as if sys wasn’t imported at all.

I’m struggling with this for days now.

Well when you have a method that takes two parameters you need to pass 2 parameters to it.

Also as mentioned you need to pass the globals() in where you are calling the function and from not the context of where the method is defined. globals() is a method that returns a dict and if you use it as a default parameters the method is evaluated at import time not call time so of course you will get same thing as you are using the same globals from the importer module as you did before when it was in the method.

your “library” code

def tryToImport(module, g):

try:

moduleObject = importlib.import_module(module)

g[module] = moduleObject

except ImportError:

print “Fail”

Using it from another place:

import importer

importer.tryToImport(“sys”, globals())

This will inject the module in the globals of the calling context and not the globals of your ‘library’ module. Again must pass the globals() in at the calling location not the declaration.

I am officially out of spoons now :wink:

···

Cody