import and package confusion

Dale Amon amon at vnl.com
Thu Apr 30 10:19:06 EDT 2009


On Thu, Apr 30, 2009 at 04:33:57AM -0300, Gabriel Genellina wrote:
> En Thu, 30 Apr 2009 03:04:40 -0300, alex23 <wuwei23 at gmail.com> escribió:
>> 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)
>>   # etc
>
> (doesn't work as written, because __import__ returns the top package when 
> given a dotted name)
> Replace the last three lines with:
>
>     __import__(packagename)
>     module = sys.modules[packagename]
>     cls = getattr(module, "Conditions")

Thanks. That works marvelously. I just knew there
had to be a better way.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: Digital signature
URL: <http://mail.python.org/pipermail/python-list/attachments/20090430/64189abe/attachment.sig>


More information about the Python-list mailing list