Load Arbitrary Script as Module?

Fredrik Lundh effbot at telia.com
Wed May 10 18:55:28 CEST 2000


Fredrik Lundh <effbot at telia.com> wrote:
> > Is there a way to load a module given a full filepath? Something like:
> > 
> > driver = LoadModule("C:\\Stuff\\sample.py")
> > driver.someFunction()
> 
> start here:
> http://www.deja.com/=dnc/getdoc.xp?AN=621152936
> 
> and add something like:
> 
> def import_file(filename):
>     name = os.path.splitext(os.path.basename(filename))[0]
>     return do_import(name, filename)

umm.  sure looks like a bot error (guess the wrong code was
in the clipboard).  here's the correct code:

def import_file(filename):
     name = os.path.splitext(os.path.basename(filename))[0]
     return do_import(name, open(filename).read())

</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