from spam import eggs, spam at runtime, how?

Rene Pijlman reply.in.the.newsgroup at my.address.is.invalid
Tue Dec 9 14:28:58 EST 2003


John J. Lee:
>Rene Pijlman:
>> How do I do:
>> 
>>    from spam import eggs
>> 
>> ... when 'spam' is only known at runtime?
>
>Why not just stick the import statement where you need it?

Because I need a module from one of a number of different packages, at one
specific point in my code.

The modules are Cheetah templates (http://www.cheetahtemplate.org/) in
different skins of a website. I've made one package per skin, and my
website generator basically does:

   from skin import template

where skin is only known at runtime (it's passed as a parameter or hidden
field to my mod_python application).

The alternative would be:

   if skin == 'basic':
       from basic import homepage
   elif skin == 'modern':
       from modern import homepage

... but this is unmaintainable and unpythonic.

-- 
René Pijlman




More information about the Python-list mailing list