Conditional Import
Gustavo Cordova
gcordova at hebmex.com
Thu Mar 14 12:13:03 EST 2002
>
> Right now, I've this hack in the middle of my code
>
> try:
> from codeop import CommandCompiler
> except:
> from MockCommandCompiler import CommandCompiler
>
> But if I have to put up with a work around like this, I'd
> prefer that it be fairly elegant. Or at least standard...
>
Well, it's *almost* standard. Just a little modification:
from exceptions import ImportError
try:
from codeop import CommandCompiler
except ImportError:
from MockCommandCompiler import CommandCompiler
and that's it. :-)
It's fairly used, look into some of the standard modules,
mainly in conjunction with StringIO/cStringIO.
Salutations!
-gustavo
More information about the Python-list
mailing list