Newbie question.. how to load modules with a dynamic name?

Fredrik Lundh fredrik at pythonware.com
Fri Feb 23 16:27:16 EST 2001


Ron Scinta wrote:
> I have a need to run various python script files from a master control
>  script, however, I do not know the names of the scripts until runtime.
>
>  I will be getting the script names from a database.. and was thinking I
>  could just use the import call with the supplied name.
>
>  This does not work alas....
>  >>> mymod = "ptw"
>  >>> import mymod
>  Traceback (innermost last):
>    File "<interactive input>", line 1, in ?
>  ImportError: No module named mym
>  >>>
>
>  What is the prefered method of doing this type of operation?

how about:

    execfile("mymod.py")

or:

    mymod = __import__("mymod")

see the library reference (builtin functions) for more info.

Cheers /F

<!-- (the eff-bot guide to) the standard python library:
http://www.pythonware.com/people/fredrik/librarybook.htm
-->






More information about the Python-list mailing list