Does pip overwrite packages installed by apt-get?

Solution 1:

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).

Solution 2:

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']