Possible to import a module whose name is contained in a variable?

Michael Hoffman cam.ac.uk at mh391.invalid
Mon Mar 7 11:23:40 CET 2005


Claudio Grondi wrote:
> STeVe,
> 
> may I ask you for more details on this?
> Any disadvantages while using exec()
> in this context?
> 
> I try to avoid using any of the
> __xxxx__() functions if possible
> (considering this a good
> programming style).

Avoiding exec (which is a statement, not a function) is much more
important. Since it executes arbitrary code, you can get unpredictable
results from it.

 >>> z = "sys; print 'w00t'"
 >>> exec "import " + z
w00t

Consider the case where z = "shutil; shutil.rmtree('/')"
-- 
Michael Hoffman



More information about the Python-list mailing list