import and package confusion
wuwei23 at gmail.com
Thu Apr 30 08:04:40 CEST 2009
On Apr 30, 1:10 pm, Dale Amon <a... at vnl.com> wrote:
> I do not really see any other way to do what I want. If
> there is a way to get rid of the exec in the sample code
> I have used, I would love to know... but I can't see how
> to import something where part of the name comes from user
> command line input without interpreting the code via exec.
Are you familiar with __import__?
iotypes = ["WINGTL","VLMPC","VLM4997"]
for iotype in iotypes:
packagename = "VLMLegacy." + iotype + ".Conditions"
classname = iotype + "_Conditions"
module = __import__(packagename)
cls = getattr(module, classname)
Much cleaner :) And far more secure: any time you think "user input"
AND "exec", you're probably going in the wrong direction...
More information about the Python-list