Search code examples
python-2.7easy-installpythonpath

Work around easy-install.pth file


How do I get a python script to import the development version of a module I'm working on (in my home directory) when that module is also defined in a system-wide easy-install.pth file?

$> more /usr/lib/python2.7/site-packages/easy-install.pth 
import sys; sys.__plen = len(sys.path)
./foo-1.1.09-py2.7-linux-x86_64.egg
import sys; new=sys.path[sys.__plen:]; del sys.path[sys.__plen:];      
p=getattr(sys,'__egginsert',0); sys.path[p:p]=new; sys.__egginsert = p+len(new)

The foo module is setup like this:

foo/
   |---- __init__.py
   |---- test.py 
   |---- module1.py
   |---- etc.

I want to execute a script from the command line, but When I do

/home/dave/src$> python foo/test.py

that script imports the version that had been installed under /usr; same thing if my working directory is /home/dave/src/foo.

Of course, I have /home/dave/src at the front of my PYTHONPATH.

What is a viable-work around for the way that the easy-install.pth gets pre-pended to the PYTHONPATH?


Solution

  • Use the -S option and an fully explicit path.

    For example

    $> PYTHONPATH=$HOME/src:/usr/lib/python2.7/site-packages python -S foo/test.py