import on modules/files that don't have .py extension
David Boddie
davidb at mcs.st-and.ac.uk
Wed Sep 17 17:44:56 EDT 2003
anthony_barker at hotmail.com (Anthony_Barker) wrote in message news:<899f842.0309170649.517e0bb9 at posting.google.com>...
> Is it possible to import modules/files that have something other than
> the .py extension?
You can use the imp module to achieve this. For example, for a module
called "mymodule" in a source file called "myfile" then
import imp
mymodule = imp.load_source("mymodule", "myfile")
should import the contents of "myfile" as a module which you can use
as normal. The "mymodule" parameter to the load_source function
presumably just ensures that
mymodule.__name__ = "mymodule"
so that anything relying on that has no nasty surprises. Note that, for this
example, you may end up with a file called "myfilec" alongside "myfile".
David
More information about the Python-list
mailing list