importing out of cwd

Matthew Dixon Cowles matt at mondoinfo.com
Sat Sep 9 17:34:28 EDT 2000


On Sat, 09 Sep 2000 14:10:11 -0700, Neil Macneale
<macneale at cats.ucsc.edu> wrote:

>What is the syntax to import something that is not in the current working
>directory.  Specifically I want to import something from one directory up.

[...]

>There must be some way to do this (without adding the top directory
>to my puthon path) that I have missed.

The usual idiom is to add the directory to your python path, albeit
temporarily:

sys.path.insert(0,"..")
import foo
del sys.path[0]

Another method that can be used is to read the contents of the file
into a string and then use Fredrik Lundh's technique for loading a
module from a string:

http://www.deja.com/getdoc.xp?AN=621152936

But there's almost never a need to do that.

Regards,
Matt



More information about the Python-list mailing list