1

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.

  • What happens when you run `PYTHONPATH="/path/to/some/where:$PYTHONPATH" python -c 'import sys; print "\n".join(sys.path)'` Do you see the new path first? – Miki Tebeka Aug 10 '12 at 05:00
  • Using pip with the `--user` option is the answer to your problem. Why do you not want to install it? Also, do you have an installation of numpy in the system-wide site-packages directory? – Keith Aug 10 '12 at 05:25
  • why don't you use `sudo apt-get install python-numpy`? – bmu Aug 10 '12 at 12:06
  • @Keith: yes, there's a system wide numpy installation that I cannot control - that's what I am trying to get around. I want my local version to be used instead –  Aug 10 '12 at 12:18
  • @Keith: I did `pip install numpy` which worked on my system although I don't have root (it does not recognize `--user`). It installed successfully but the system wide version in /usr/ is still read... any ideas? –  Aug 10 '12 at 12:25
  • @user248237 if you cannot install an up to date system package (12.04 has 1.6.1) or no admin rights, I would install it in a virtual environment. I recently described my work flow here: http://stackoverflow.com/a/11864996/1301710 – bmu Aug 10 '12 at 13:25
  • Apparently nobody spotted this until now: the command given at the start, `export PTYHONPATH=...` has a typo (`PTYHON`, not `PYTHON`). – swineone Feb 16 '23 at 03:15

1 Answers1

2

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"
Keith
  • 42,110
  • 11
  • 57
  • 76
  • I just tried `pip 1.1` with `--user` and it still does not install things locally. If I do: `pip install --user pandas`, it fails with `OSError: [Errno 13] Permission denied: '/usr/bin/f2py2.6'`. It still tries to change system wide directories ... –  Aug 10 '12 at 15:25
  • This directory does not get placed first... the system paths are first –  Sep 07 '12 at 00:40
  • @user248237 That can't be, since the site.py code explicitly places the user site directory before the system site directory. Unless your site administrator is doing something really odd. – Keith Sep 07 '12 at 10:08