How can I import a script with an arbitrary name ?

dakman at gmail.com dakman at gmail.com
Mon Oct 30 18:38:34 CET 2006


I had to do something like this a while back for a modular IRC bot that
I wrote.
__import__() will do the trick, however to avoid getting a cache of the
module I recomend doing something like...

mod = reload( __import__("%s-%s-%s" % ( t[0], t[1], t[2] ) ) )


leonel.gayard at gmail.com wrote:
> Hi all,
>
> 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.
> How can I do this ?  In my example, the last line will obviously not
> work.




More information about the Python-list mailing list