Mailing List Archive

import from user input?
Hello,

I want to import modules at runtime, and get the module name from the user.
Is that possible? How?

Another question: is there a function to copy/move entire directory trees?

TIA,
______________________________________________________
Gaetan Corneau
Software Developer (System integration Team)
BaaN Supply Chain Solutions
E-mail: Gaetan_Corneau@baan.com
Compuserve: Gaetan_Corneau@compuserve.com
ICQ Number: 7395494
Tel: (418) 654-1454 ext. 252
______________________________________________________
"Profanity is the one language all programmers know best"
import from user input? [ In reply to ]
Gaetan Corneau wrote:
>
> Hello,
>
> I want to import modules at runtime, and get the module name from the user.
> Is that possible? How?

There is more than one way.
The obvious one is to execute a generated statement
in the correct environment, as in

>>> import string
>>> modname=string.strip(raw_input("which module do you want to import today?"))
>>> exec "import "+modname
>>> re # what I typed
<module 're' from 'D:\Python\Lib\re.pyc'>
>>>

But if you want to be less open to users who might type bad things
like "sys ; sys.exit()",
this version might be easier to handle:

>>> globals()[modname] = __import__(modname)

The advantage is that the builtin function __import__ is a function
which requires a module name as a string parameter.
No necessity to check for bad input, but by a try...except ImportError
clause
if the module isn't found.

> Another question: is there a function to copy/move entire directory trees?

There is a walk function in os.path which makes it
an easy exercise to write such a function.

ciao - chris

--
Christian Tismer :^) <mailto:tismer@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
import from user input? [ In reply to ]
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.

> > Another question: is there a function to copy/move entire directory
> trees?
> There is a walk function in os.path which makes it
> an easy exercise to write such a function.
[GC]
Thanks a lot, Chris.
______________________________________________________
Gaetan Corneau
Software Developer (System integration Team)
BaaN Supply Chain Solutions
E-mail: Gaetan_Corneau@baan.com
Compuserve: Gaetan_Corneau@compuserve.com
ICQ Number: 7395494
Tel: (418) 654-1454 ext. 252
______________________________________________________
"Profanity is the one language all programmers know best"
import from user input? [ In reply to ]
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@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
import from user input? [ In reply to ]
Christian Tismer writes:
>
> import string
> globals()[string.split(modname, ".")[0]] = __import__(modname)
>
> seems to do it better.
>

Why not just

exec "import "+modname

?
import from user input? [ In reply to ]
Hi!

> > Another question: is there a function to copy/move entire directory trees?

There are functions in shutil.py; look for copytree()...

Oleg.
----
Oleg Broytmann National Research Surgery Centre http://sun.med.ru/~phd/
Programmers don't die, they just GOSUB without RETURN.
import from user input? [ In reply to ]
Charles G Waldman wrote:
>
> Christian Tismer writes:
> >
> > import string
> > globals()[string.split(modname, ".")[0]] = __import__(modname)
> >
> > seems to do it better.
> >
>
> Why not just
>
> exec "import "+modname

See my former post.
It works for you and me, but if I have to be aware of
users trying things like

modname = "sys;sys.exit()"

to name a quite harmless idea, you would have more work to
prevend this than by a string which is no parsed command.
I think it's not clean to give the user full access to your
namespace and interpreter. Not if you are the user, of course.
But if you allow arbitrary strings to be executed, you are poking
a big hole into your software. How about

modname = "sys;None=5"

This was just a concern, which would more apply to Internet
CGI scripts. Using these concepts thoughtlessly with the proper
pickled string, would let your user break into your module
completely.

ciao - chris

--
Christian Tismer :^) <mailto:tismer@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