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

Mark Hammond mhammond at skippinet.com.au
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/customer.py
> 	/myproj/presentation/layout.py
> 	/myproj/data/tabular.py
> 
> and you have a module in myproj that needs to use modules in biz,
> presentation, and data:
> 
> 	from myproj.biz import customer
> 	from myproj.presentation import layout
> 	from myproj.data 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 '__init__.py' (which can 
be empty or contain just a comment, for example).

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

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

Mark.




More information about the Python-list mailing list