Correctness: sys.path.append("..")

Mark Hammond mhammond at
Thu Jan 10 03:35:41 CET 2002

Mark McEahern wrote:

> Jeremy Bowers
>>I've taken to sticking this on top of my files in the subdirectories:
>>import sys
>>if ".." not in sys.path:
>>	sys.path.append("..")
> Suppose you have the following structure:
> 	/myproj/biz/
> 	/myproj/presentation/
> 	/myproj/data/
> and you have a module in myproj that needs to use modules in biz,
> presentation, and data:
> 	from import customer
> 	from myproj.presentation import layout
> 	from import tabular
> That to me seems preferable to mucking with sys.path.

Just to clarify what the other Mark said ;)  The above will work as long as:
* .\myproj and each of its sub-dirs have a file '' (which can 
be empty or contain just a comment, for example).

* The parent of .\myproj is on sys.path.

This makes 'myproj' a package, etc (sub-)packages, and a (sub-)module.  In many cases, you only have a 
top-level package with sub-modules, rather than multiple sub-packages.


More information about the Python-list mailing list