Manipulating sys.path

Thomas thomas.bugzilla at gmx.net
Wed Jul 9 06:22:46 EDT 2008


Hi all,

I want to manipulate sys.path outside of PYTHONPATH (environment) and 
sys.path.append() (programmatically).

A bit of background:
We're maintaining a small application that includes a couple of Python 
scripts. Over time, a decent amount of code has been forked into 
modules, so the overall file system layout of our kit looks like this:

tool/
   bin/
     prog1.py
     prog2.py
     ...
   lib/
     pack1/
       mod1.py
       mod2.py
       ...

The issue I have is that I want to add the 'lib' directory to the module 
search path so that our programs prog1.py, prog2.py,... can find the 
modules pack1.mod1, pack1.mod2, ... But I want to keep this out of the 
program's source code which rules out statements like 
'sys.path.insert(0, "../lib")'. We also want to be minimal-invasive for 
the hosting environment, so no copying of 'lib' into the standard Python 
lib directories (like /usr/local/lib/python2.5/site-packages etc.), nor 
forcing the user to change his PYTHONPATH shell environment. It should 
be solved locally in our kit's directory tree.

I was thinking about putting code into a 'bin/__init__.py' file but 
that's only working for modules and not for executable scripts, right?! 
Then I came across the '.pth' files, but unfortunately they only seem to 
work in some standard paths (like the before mentioned 
/usr/local/lib/python2.5/site-packages), and not in the script directory 
(like 'bin' in my case) which is automatically added to sys.path.

Can anybody think of something that could be of help here?

Thanks,
Thomas



More information about the Python-list mailing list