How can I import a script with an arbitrary name ?

Fredrik Lundh fredrik at pythonware.com
Mon Oct 30 10:30:37 EST 2006


leonel.gayard at gmail.com wrote:

> I have a script responsible for loading and executing scripts on a
> daily basis. Something like this:
> 
> import time
> t = time.gmtime()
> filename = t[0] + '-' + t[1] + '-' + t[2] + '.py'
> import filename
> 
> So, I have a module with an arbitrary file name and I want to load it,
> and later access its function definitions.

execfile() is probably your best bet:

     namespace = {}
     execfile(filename, namespace)

     namespace["function"](argument)

also see:

     http://effbot.org/zone/import-string.htm#importing-by-filename

</F>




More information about the Python-list mailing list