R
Rene Pijlman
How do I do:
from spam import eggs
.... when 'spam' is only known at runtime?
from spam import eggs
.... when 'spam' is only known at runtime?
How do I do:
from spam import eggs
... when 'spam' is only known at runtime?
spam = __import__('spam')
eggs = spam.eggs
Rene said:Hmm... my spam is a package and eggs is a module. This code gives me:
AttributeError: 'spam' module has no attribute 'eggs'
But it works like this:
spam = __import__('spam',globals(),locals(),['eggs'])
eggs = spam.eggs
Rene Pijlman:But it works like this:
spam = __import__('spam',globals(),locals(),['eggs'])
eggs = spam.eggs
or:
spam = __import__('spam.eggs')
eggs = spam.eggs
Rene Pijlman said:Yes, and a bit more elegant (pythonic?). Thanks.
Another way:
exec 'import spam'
That's precisely what we were trying to avoid.
Fredrik" == Fredrik Lundh said:spam = __import__('spam.eggs')
eggs = spam.eggs
Is there a way to do this if I don't know about "eggs" beforehand. I have
some code which needs to import a DB module dynamically. The module could be
'PyPgSQL.PgSQL' or 'sqlite'. Is there a sane way to do this without exec?
Ganesan R said:Is there a way to do this if I don't know about "eggs" beforehand. I have
some code which needs to import a DB module dynamically. The module could be
'PyPgSQL.PgSQL' or 'sqlite'. Is there a sane way to do this without exec?
Rene Pijlman said:How do I do:
from spam import eggs
... when 'spam' is only known at runtime?
Rene Pijlman:
Why not just stick the import statement where you need it?
Rene Pijlman said:Because I need a module from one of a number of different packages, at one
specific point in my code.
Paul said:I still don't understand why you don't want to use an exec statement
for this. That's by far the most natural and understandable way to do it.
Rene said: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.
Peter Hansen said:In this case, he's getting the actual string from the web, so I wouldn't
be surprised if exec would be a real can of security worms.
Also, __import__ returns a module object, so you don't have to pull it
out of the globals you passed to exec. Compare:
g = {}
exec "import %s" % modulename in d
m = g[modulename]
with
m = __import__(modulename)
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.
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.