Loading a module by expanding a string variable?

alex k alexander_k at my-deja.com
Fri Oct 27 10:01:33 EDT 2000


or:
mod = module_you_want

exec 'import ' + mod
(altarnatively)
exec 'from ' + mod + ' import *'

 / alex

In article <k0nlmvb1db8.fsf at dhcp-49138.phys.ntnu.no>,
  Joakim Hove <hove at phys.ntnu.no> wrote:
> "Fredrik Lundh" <effbot at telia.com> writes:
>
> > if you trust whoever sets the module variable, and don't mind
> > compiling code on the fly, you can also use exec:
> >
> >     exec "from %s import *" % module
>
> This did the trick - thanks a lot.
>
> > (if you don't trust the source, consider what happens if module is
> > set to e.g "os import *; system('rm -rf $HOME') #")
>
> I write the modules myself - so I guess I have to trust the source -
> but thanks for the warning anyway.
>
> Joakim
>
> --
> === Joakim Hove      www.phys.ntnu.no/~hove/   ======================
> # Institutt for fysikk  (735) 93637 / E3-166  |  Skøyensgate 10D    #
> # N - 7491 Trondheim    hove at phys.ntnu.no     |  N - 7030 Trondheim #
> ================================================ 73 93 31 68 ========
>

--
.
.
...: [ ~~~~~~~ ] :...


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list