importing module through variable
Thomas Wouters
thomas at xs4all.net
Tue Jun 13 10:06:06 EDT 2000
On Tue, Jun 13, 2000 at 03:51:25PM +0100, André Dahlqvist wrote:
> I have a variable which is assigned the name of a module. Is it possible
> to use this variable in an import statement? Someone told me I could use
> "exec("import %s" %var) and while this works I am not sure how I can
> access the module by it's name after that. Using something like
> var.modfunc() won't do it since 'var' is just a string containing the
> modules name.
You want the __import__ function:
module = __import__(var)
>>> print __import__.__doc__
__import__(name, globals, locals, fromlist) -> module
Import a module. The globals are only used to determine the context;
they are not modified. The locals are currently unused. The fromlist
should be a list of names to emulate ``from name import ...'', or an
empty list to emulate ``import name''.
When importing a module from a package, note that __import__('A.B', ...)
returns package A when fromlist is empty, but its submodule B when
--
Thomas Wouters <thomas at xs4all.net>
Hi! I'm a .signature virus! copy me into your .signature file to help me spread!
More information about the Python-list
mailing list