Search code examples
pythonnumpypipsetuptoolsdistutils

Pythonpath is still ignored and unable to install locally with pip


I'm finding that my pythonpath environment variable is ignored. I'm using python 2.6 on ubuntu. I have in my .bashrc the following:

export PTYHONPATH=/my/home/mylibs/lib/python2.6/site-packages/:$PYTHONPATH

Then I install a new version of numpy using:

python setup.py install --prefix=/my/home/mylibs/

and it gets correctly installed locally. However, when I try to install other packages (also using setup.py) that depend on the new version of numpy, they cannot find it, because by default the loaded numpy is the one in /usr/llib, and not the one specified in my PYTHONPATH. My PYTHONPATH gets correctly set but the system-wide directory is still overruling it.

How can this be fixed? I just want my local version of numpy to be accessed when I do import numpy. I saw other posts related to this with python 2.4 but as far as I can tell it never got resolved. Also, i'd like to do this without installing pip or virtualenv for now. It seems like it should be possible using --prefix or --home options passed to setup.py and then alteration of PYTHONPATH but this does not work for me... the system wide lib dirs are read first.

edit: I try to follow the suggestions and use pip. I have a system wide install of an old pip that does not recognize --user (ver 0.3). I tried to upgrade pip with pip itself but of course that failed because I cannot install it locally, so pip install pip --upgrade --user is not an option. I downloaded a new version of pip and installed locally in my home directory but the system wide old one is still used when I type pip at the prompt. I looked into the pip package and found runner.py so I tried to use it to install packages using:

runner.py install --user numpy --upgrade

That still fails with permission denied:

OSError: [Errno 13] Permission denied: '/usr/bin/f2py2.6'

It looks like --user is broken. I also am not sure how this would solve the fact that the system wide python uses the system wide packages in /usr/lib... is there a solution to this? It seems like it's virtually impossible to install local packages in python nowadays.


Solution

  • Ok, Python will use the first package it finds. The PYTHONPATH gets appended to sys.path, after the system one. So it will normally find the system one first. But the "official" per-user packages directory seems to be placed before that. So create your personal site-packages directory:

    mkdir -p $HOME/.local/lib64/python2.7/site-packages
    mkdir $HOME/bin
    

    (You may have to change "lib64" to "lib32" or just "lib")

    This directory gets placed before the system one on my system. But you should verify it by printing out sys.path.

    Then install your packages into there. However, the --user option in the latest pip version should already place it there.

    As a list resort you can manipulate sys.path. You can insert your directory into sys.path before the system site-packages, then import numpy.

    You are getting permissions errors from the scripts installation, trying to put that in the system location. You can pass additional options to install scripts in your $HOME/bin directory.

    Install like this:

    pip install --user --install-option="--install-scripts=$HOME/bin"