How to make a conditional import???
Alex Martelli
aleaxit at yahoo.com
Fri Nov 10 11:04:59 EST 2000
"Peter Arwanitis" <arwanitis at iabg.de> wrote in message
news:8ugvjf$25p$1 at news2.iabg.de...
[snip]
> def selectProjectType():
> #... skipped
> if projectType == 'phys':
> from pyhsModule import *
> else:
> from logicModule import *
[snip]
> everybody know the effect... the import statement in selectProjectType()
is
> only processed locally!
Yep.
> But I need it global, like the string import...
>
> PLEASE: I need a quick solution, and not a code-philosophy-thread :-)
> I know, that I can make it better on other ways (and without from...)
Yeah, the from is the killer -- else, a simple 'global' for the
modulename you're importing should suffice.
> BUT is there a obfuscated solution to make this import happen in global
> scope???
It's not so much obfuscated as gross, but...:
using
exec "from logicModule import *" in globals()
instead of
from logicModule import *
should, I think, give you the effect you desire (*shudder*).
Or else, if you know you're in a module named 'silly'...:
import silly
import logicModule
silly.__dict__.update(logicModule.__dict__)
I hope *this* one is sufficiently obfuscated for you...:-).
There are several variations possible on these (bad)
ideas, of course.
Alex
More information about the Python-list
mailing list