Loading Modules Dynamically at RunTime

Olivier Dagenais olivierS.dagenaisP at canadaA.comM
Sun Oct 29 11:54:07 EST 2000


I would recommend using the exec function if you have the code already in a
string (if you loaded it from a database or something) or the execfile
function if you want to point to files (like the result of an HTTP
transfer).

--
----------------------------------------------------------------------
Olivier A. Dagenais - Software Architect and Developer
"Someone called 'Type your name here' is impersonating me on the
internet and is posting exactly the same things I am posting!"


"Steve" <SCook at pobox.com> wrote in message
news:JKQK5.3241$Tq6.33927 at news-server.bigpond.net.au...
> Hi,
>
> I need to create classes at run time based on a config file.
> If the file contains say
>
> foo
> bar
>
> Then foo and bar are the names of two python modules contains the class
> definition of foo and bar.  So at runtime after reading the names foo and
> bar, I need to import the foo.py and bar.py but so far I haven't been able
> to do this.  Both my attempts failed, I tried
>
> import foo + ".py"
>
> and
>
> modulename = "foo.py"
> impot modulename
>
> and
>
> imp = "import foo.py"
> eval(imp)
>
> They all fail to work, what is the correct way to import at runtime
without
> knowing the name of the module at compile time?
>
> Regards,
> Steve
>
>





More information about the Python-list mailing list