import from user input?

Christian Tismer tismer at appliedbiometrics.com
Fri Apr 9 10:31:45 EDT 1999


Gaetan Corneau wrote:
> 
> Chris,
> 
> > >>> globals()[modname] = __import__(modname)
> [GC]
> Exactly what I was looking for. I read about the __import__ function in the
> doc, but I didn't know that I had to assign it that way.

Actually, I did no good job by suggesting that. It will work
for simple module names. But if you try with things like
"win32com.client", it will break. The import will work,
but the module will not be accessible in the way a user would
expect.

Instead, one should cut off the first name before the first dot
and put that into globals().

import string
globals()[string.split(modname, ".")[0]] = __import__(modname)

seems to do it better.

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaiserin-Augusta-Allee 101   :    *Starship* http://starship.python.net
10553 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     we're tired of banana software - shipped green, ripens at home




More information about the Python-list mailing list