Absolute imports?

Terry Reedy tjreedy at udel.edu
Sat Jan 8 16:33:50 EST 2011


On 1/8/2011 3:03 PM, Roy Smith wrote:
> If I have an absolute path to a file (i.e. '/home/roy/foo.py'), is there
> a way to import that as a module WITHOUT modifying sys.path?  I'm using
> Python 2.6.

Import from another file in /home/roy. (since '.' is part of sys.path).
Or put module or package of modules in Lib/site-packages.
But why the horror of modifying sys.path? It is normal proceedure.

> I've read PEP 328, and don't really understand how the absolute imports
> it's talking about are supposed to work.

Those are the normal imports that start from a directory in sys.path. 
Relative imports (now) are ones that use '.'s to locate relative to the 
importing module. I have never done that. Purpose is to make a 
subpackage relocatable to another package without modification.

> Should I be using imp.load_source()?

No idea, never done that. Try it if you want.

-- 
Terry Jan Reedy




More information about the Python-list mailing list