views:

670

answers:

7
+1  Q: 

PYTHONPATH ignored

Environment: debian 4.0

Python 2.4

My 'project' is installed in:

/usr/lib/python2.4/site-packages/project.

But I want to use my working copy instead of the installed one which is located in:

/home/me/dev/project/src

So what I do is:

export PYTHONPATH=/home/me/dev/project/src

ipython

import foo # which is in src

foo.__file__

*/usr/lib/python2.4/site-packages/project/foo.py*

instead of :

/home/me/dev/project/src/project/foo.py

How come? I try to check the pathes (having done the export above) and what I see is:

import sys,os

sys.path

['', '/usr/bin', '/usr/lib/python2.4/site-packages', '/home/me/dev/project/src', '/usr/lib/python24.zip', '/usr/lib/python2.4', '/usr/lib/python2.4/plat-linux2', '/usr/lib/python2.4/lib-tk', '/usr/lib/python2.4/lib-dynload', '/usr/local/lib/python2.4/site-packages', '/usr/lib/python2.4/site-packages/PIL', '/var/lib/python-support/python2.4', '/usr/lib/python2.4/site-packages/IPython/Extensions', '/home/me/.ipython']

os.environ['PYTHONPATH']

/home/me/dev/project/src

A: 

I think you set up PYTHONPATH to /home/me/build/project/src since /home/me/dev/project/src does not appear in sys.path, but /home/me/build/project/src does.

David Cournapeau
Thanks David. sys.path does include /home/me/dev/project/src instead /home/me/build/project/src (as I've renamed some of the stuff there was a mistake in there). The issue persists.
+1  A: 

I see '/usr/lib/python2.4/site-packages' in your path prior to '/home/me/dev/project/src', does that matter? What happens when you switch the two?

From the docs:

When PYTHONPATH is not set, or when the file is not found there, the search continues in an installation-dependent default path

So perhaps you didn't find your working copy on your PYTHONPATH as you had thought?

John Pirie
I think John is right, Python will use the module with the lowest index in sys.path
wodemoneke
Yes, that matters. PYTHON, AFAIK, should insert the PYTHONPATH first (or just after '.')My PYTHONPATH is correct.
But if you've made an error, such as omitting the __init__.py file in /home/me/dev/project/src, then python will go on to find the prod package in site-packages
John Pirie
`__init__.py` is there ( in `/home/me/dev/project/src` )
Have you tried swapping the two items in your sys.path that I suggested?
John Pirie
@John. If I do that it works. The thing is that I don't want to do it. It has to be done from outside the python code.
A: 

I don't believe you have any control over where the PYTHONPATH gets inserted into the actual path list. But that's not the only way to modify the path - you can update sys.path yourself, before you try to import the project.

Edit: In your specific case, you can modify the path with

import sys
sys.path.insert(2, '/home/me/dev/project/src')
Mark Ransom
So I can't force python where to look for modules?
I know that. But I don't want to touch at the code.
+4  A: 

According to python documentation, this is expected behavior: http://www.python.org/doc/2.4.4/lib/module-sys.html:

Notice that the script directory is inserted before the entries inserted as a result of PYTHONPATH.

Under python-2.6 it is different: http://docs.python.org/tutorial/modules.html#the-module-search-path

van
I'm fine with the eggs. :) But why '/usr/lib/python2.4/site-packages' comes before my python path? How can I force the PYTHONPATH to be of more priority than the '/usr/lib/python2.4/site-packages' without changing the sys.path within the python file?
@joseph: updated the answer.
van
@van. I don't get it. I set the PYTHONPATH to something correct and it loads first what's in the site-packages. Bizarre!
To change the priority, don't run a script that's located in site-packages. Move the script anywhere else and the script's directory (site-packages) will no longer mess up your intended search.
S.Lott
Scripts directory? I'm importing a module which is both in site-packages/project/foo.py and /home/me/dev/project/src/project/foo.py. For the sake of solving this, let's assume I want to be able to run it with ipython.
@joseph. see the links I gave: for python-2.4 it is actually the defined behavior. It is changed since then.
van
@Jospeh: the top-level script, which contains the import statement, has a directory that is inserted at the head of the sys.path.
S.Lott
A: 

It sounds like the src directory doesn't have an __init__.py file. It's not a proper package.

S.Lott
As you'll see above, there is one.
@S.lott sorry. What I said is "__init__.py` is there ( in `/home/me/dev/project/src` )"
A: 

Not a direct answer to you question, but you could also use a virtualenv to create a development environment. In that virtualenv you can then install your product in /home/me/dev/project/src as a development package: "python setup.py develop".

This way you don't have to change your PYTHONPATH manually. Just activate the virtualenv if you want to use the development code.

Mark van Lent
A: 

I found the problem (I've missed early on when somebody pointed me to http://stackoverflow.com/questions/897792/pythons-sys-path-value).

It seems that easy_install creates a pth file /usr/lib/python2.4/site-packages/easy-install.pth which is then loaded by site.py. This inserts the site-packages path in the sys path before the PYTHONPATH. Not nice.