path to modules per import statement

Peter Hansen peter at engcorp.com
Fri Mar 24 10:58:25 EST 2006


AndyL wrote:
> danmcleran at yahoo.com wrote:
> I work on rather big set of Python applications: something like 100 .py 
> files divided into libs and separate sub-applications.
> 
> For now I keep almost everything in one directory but I wish following 
> structure to be in place:
> 
> 	app1/  app2/  lib1/  lib2/  lib3/
> 
> 
> and be able to import from each app[12] all the libs. I do not want to 
> touch existing code to prefix all the import places with lib[123] nether 
> I want to play with sys.path.append too much.

You should use Fuzzyman's solution of __init__.py files (look in the 
Python documentation for information about "packages"... I believe it's 
even covered adequately in the tutorial, so maybe rereading that would 
help), but also look into the use of ".pth" files.  This is documented 
in the online help for the "site" standard library module, or in the 
helpful comments in the source in python's lib/site.py file.

Basically to find the packages in lib1/ and friends regardless of what 
the current directory is, you need to use either the PYTHONPATH 
environment variable, modify sys.path directly (and it is acceptable to 
do that in various circumstances), or use .pth files as documented to 
get the desired effect.

-Peter




More information about the Python-list mailing list