6

I have numpy 1.8.2 installed in /usr/lib/python3/dist-packages (via apt), and the newest version (1.9.2) installed in /usr/local/lib/python3.4/dist-packages (via pip). Both paths are in sys.path (in that order), but only the older apt version is getting imported in python3.

➜  ~  sudo pip3 install --upgrade numpy
[sudo] password for naught101: 
Real name of requirement numpy is numpy
Requirement already up-to-date: numpy in /usr/local/lib/python3.4/dist-packages
Cleaning up...
➜  ~  ipython3
Python 3.4.0 (default, Apr 11 2014, 13:05:11) 
Type "copyright", "credits" or "license" for more information.

IPython 1.2.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: import numpy

In [2]: numpy.version
Out[2]: <module 'numpy.version' from '/usr/lib/python3/dist-packages/numpy/version.py'>

In [3]: 
KeyboardInterrupt

In [3]: numpy.version.version
Out[3]: '1.8.2'

In [5]: import sys

In [6]: sys.path
Out[6]: 
['',
 '/usr/local/bin',
 '/usr/lib/python3/dist-packages',
 '/usr/lib/python3.4',
 '/usr/lib/python3.4/plat-x86_64-linux-gnu',
 '/usr/lib/python3.4/lib-dynload',
 '/usr/local/lib/python3.4/dist-packages',
 '/usr/lib/python3.4/dist-packages',
 '/usr/lib/python3/dist-packages/IPython/extensions']

(python3 does the same thing as ipython3 here)

How can I get python3 to use the newer version?

I can't just uninstall the apt version, because other packages depend on it.

  • I think the python/ipython version is different to what pip3 is installing to. Did you install ipython3 via pip? – hayd Mar 12 '15 at 2:49
  • Hrm, no, it looks like it's system installed. – naught101 Mar 12 '15 at 3:24
  • Any solution to this? As of Ubuntu 15.10, and using a python setup.py install from a git checkout over the ubuntu repository version, I still experience the same problem. I'd have argued that /usr/local/lib should always override /usr/lib, why are they reversed here? – burnpanck Oct 26 '15 at 17:53
  • Use conda. Avoid pip, especially to install system packages. Leave the system python for apt to deal with. – naught101 Oct 26 '15 at 23:50
2

This blog post explain the code logic behind this very well : https://leemendelowitz.github.io/blog/how-does-python-find-packages.html

Quoting it:

As the docs explain, sys.path is populated using the current working directory, followed by directories listed in your PYTHONPATH environment variable, followed by installation-dependent default paths, which are controlled by the site module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.