[Tutor] import by filename

Alan Gauld alan.gauld at blueyonder.co.uk
Thu Aug 26 10:32:20 CEST 2004

> think this might be it.  However I am not finding this particularly
> transparent.  What I'm worried about is whether or not it needs to
> have a python extension.
> *load_source*( name, pathname, file)
> Load and initialize a module implemented as a Python source file and
> return its module object. If the module was already initialized, it
> will
> be initialized /again/. The name argument is used to create or
> a
> module object. The pathname argument points to the source file. The
> file
> argument is the source file, open for reading as text, from the
> beginning. It must currently be a real file object, not a
> user-defined
> class emulating a file. Note that if a properly matching
> byte-compiled
> file (with suffix .pyc or .pyo) exists, it will be used instead of
> parsing the given source file.

Since you don;t show us any code that you've triedits hard to know
what you don't understand. But the way I read that description
I'd guess a typical call sequence would look like:

myfile = open('/a/long/file/name/to/file.ext')

load_source('mymodule',   # the name by which we will refer to it in
our code
            '/a/long/file/name/to/file.ext',  # the full pathname
            myFile)   # the open file object

mymodule.foo()  # a call to a function in the new module

Is that what you are doing?
What happens exactly?

Alan G.

More information about the Tutor mailing list