How to import from within a module

Donn Cave donn at u.washington.edu
Fri Dec 29 12:27:28 EST 2000


Quoth Le Snelson <le_snelson at transtechinc.cc>:
...
| I am unable to get __import__( xxx ) to actually add xxx to any visible
| dictionary. However, in the same file if I uncomment a line at the top
| (global scope) that reads:
| import xxx
| Sure enough the xxx is in the list from dir(init).
|
| I have tried all combinations of __import__(xxx, globals(), locals())
| __import__(xxx, globals(), globals()) etc. I have also tried the "import
| xxx" within the function I'm working on.  I expected syntax error but
| got none as well as no discernible result.
|
| Do I need to use the imp module to get this module found and loaded?

I hope you will laugh at how simple it is:

      xxx = __import__('xxx')

Now, I don't use this commonly and may have missed something, but
that seems to work for me.  If you type __import__('string')
interactively, the interpreter prints out its function return, so
it would have been easy to see there.

	Donn Cave, donn at u.washington.edu



More information about the Python-list mailing list