12

What happens if I accidentally pip install a package that I already installed using apt-get beforehand? Do I simply get to use the latest version (i.e. probably the one I installed using pip)? What happens the other way round?

Note: I do not use Python in a virtual environment (simply because I don't know why I should -- I use Python as a data analysis tool and mostly try to use the latest version of everything).

4
  • If I'm not wrong, you do not install the same packages using apt-get and pip
    – Lucio
    May 31, 2014 at 0:30
  • Corerct, pip installs them in /usr/local subdirectories by default or any other directory if you want.
    – Timo
    May 31, 2014 at 11:44
  • 3
    But what happens, for instance, if I first do apt-get install python-pandas followed by pip install pandas, or the other way round? How does my global python installation know which of both versions to use?
    – Fred S
    May 31, 2014 at 14:55
  • 1
    @FredS: Run the following command in the terminal, Python will search in that specific order for a module: python -c "import sys; print sys.path"
    – Timo
    Jun 2, 2014 at 11:11

2 Answers 2

6

As @Timo mentioned in his comment, python -c "import sys; print sys.path" will give you the Python module load path for your install of Python. On a relatively fresh install of 16.04, there are three directories of note (among the 10 total that were in mine):

  • '' (application's directory)
  • '/usr/local/lib/python2.7/dist-packages' (where pip installs modules)
  • '/usr/lib/python2.7/dist-packages' (where apt install modules)

Most important to note, however, is that '/usr/local/lib/python2.7/dist-packages' is HIGHER in precedence than '/usr/lib/python2.7/dist-packages'.

If you first install a Python package via apt(-get), it will install into '/usr/lib/python2.7/dist-packages'. If you later try and use pip to install the module, pip will initially give a warning that the dependency is already met, then exit; adding the --upgrade flag will force pip to install, installing the module into '/usr/local/lib/python2.7/dist-packages'. (Notice the output that also says that the version in '/usr/lib/python2.7/dist-packages' is not uninstalled) You can then verify that the pip-installed version is the one being used by using the following: python -c "import MODULE; print MODULE.__file__".

As such, this shows that packages installed via pip will take precedence over system-installed packages, but won't overwrite anything installed from apt(-get).

1

To add to @Bryan Wyatt, it seems desirable (and intended) that PIP installed/upgraded items should take precedence over (likely older) APT installed packages. My system had the apt and pip paths reversed. It should be (ignoring other entries):

  • '/usr/local/lib/python2.7/dist-packages' (where pip installs modules)
  • '/usr/lib/python2.7/dist-packages' (where apt install modules)

Yet due to some unknown action I must have taken, these paths appeared in the opposite order for me (ignoring other entries):

  • '/usr/lib/python2.7/dist-packages' (where apt install modules)
  • '/usr/local/lib/python2.7/dist-packages' (where pip installs modules)

It turns out something I did added /usr/lib/python2.7/dist-packages to /usr/local/lib/python2.7/dist-packages/easy-install.pth. Simply removing the line from easy-install.pth fixed the misordering for me. /usr/lib/python2.7/dist-packages is still in my path, since it gets added at a later stage elsewhere.

As a side note, pprint will display your path nicer...ie:

$ python -c "import sys; import pprint; pprint.pprint(sys.path)"
['',
 '/usr/lib/python2.7',
 '/usr/lib/python2.7/plat-x86_64-linux-gnu',
 '/usr/lib/python2.7/lib-tk',
 '/usr/lib/python2.7/lib-old',
 '/usr/lib/python2.7/lib-dynload',
 '/usr/local/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages',
 '/usr/lib/python2.7/dist-packages/PILcompat',
 '/usr/lib/python2.7/dist-packages/gtk-2.0',
 '/usr/lib/python2.7/dist-packages/ubuntu-sso-client']

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .