how to import from variable source

Brian Quinlan brian at sweetapp.com
Sun Apr 15 04:11:45 EDT 2001


There is probably a better strategy but it is hard to guess what that would
be based on the information given.

That said, there is a built-in function named "__import__" that does what
you want:

>>> 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
fromlist is not empty.

-----Original Message-----
From: python-list-admin at python.org
[mailto:python-list-admin at python.org]On Behalf Of Rob Brown-Bayliss
Sent: Saturday, April 14, 2001 12:35 PM
To: python-list at python.org
Cc: python-list at python.org
Subject: how to import from variable source


Hello

I am building a shell type app that imports modules depending on the
users actions.

I do not want to have the import routines hard coded, rather I would
like to import from a variable like so:

module = 'mymod'
import module

Only, as I found out I get an error stating that there is not module
called module.

Is this possible in python?  Is there a better way perhapse?

Thanks,


--

  Rob Brown-Bayliss
 ---======o======---
  www.ZOOstation.cc

--
http://mail.python.org/mailman/listinfo/python-list





More information about the Python-list mailing list