Lawson Hanson wrote:
So is there any way to get Python to import the named module
without just doing "from dummy import *", because I will
not know that the user wants to use the "dummy" module
until run-time ... I'm trying to import control data
for different run scenarios which will be defined in
differently named Python modules which the user will
specify at run-time with a command-line option
And help with this would be most appreciated
For the sake of humanity, I must try to talk you out of this. Well, it's
not that serious, but dynamic import confuses programs that inspect code
like py2exe, etc. I think it is likely that you will find the day that
you regret trying so hard to 'import *' dynamically. A more maintainable
way is to simply map the command line argument to an import statement
and keep your namespaces clean:
"""importer_module"""
import sys
import dummy
import bonafide
modules = {"dummy" : dummy, "bonafide" : bonafide}
module = modules[sys.argv[1]]
If you have several modules that themselves might need the conditional
imports, simply put the import statements in a separate module (e.g.
"importer_module" and do something like
"""another_module"""
from importer_module import module
If you simply don't want to import a bunch of modules, use an if/then
statement. In any event, I advise you to not design your code or usage
around dynamic imports using the __import__() statement or exec().
James
--
James Stroud
UCLA-DOE Institute for Genomics and Proteomics
Box 951570
Los Angeles, CA 90095
http://www.jamesstroud.com
--
http://mail.python.org/mailman/listinfo/python-list