How to determine which python modules were installed with `pip` and which with `apt`?

I was trying to fix my problem with ReText (installed system-wide with pip3).
Two of my 16.04 LTS systems have different output of pip3 list.

I know that pip3 list shows all packages (installed with both pip3 and apt/apt-get).

Also I know that

  • APT installs stuff to /usr/lib/python3/dist-packages;
  • pip3 installs system-wide stuff to /usr/local/lib/python3.5/dist-packages.

How to determine which python modules were installed with pip and which with apt?


Not cast in stone but from this Ask Ubuntu Q&A:

As @Radu Rădeanu pointed out in this answer, there would generally be difference in names of packages as well. Canonical usually names Python 2 packages as python- and Python 3 packages as python3-. Whereas for pip we generally just need to use for both Python 2 as well as Python3 packages.

Generally speaking then:

  • If the package name starts with python- or python3- it was installed by apt.
  • Otherwise the package was installed by pip

I just came across the same question. A first, brute force, approach I though of is getting both lists, and programmatically get the difference. In pseudocode:

pkg_all = $(pip3 list)
pkg_apt = $(dpkg -l | grep python3)
pkg_pip = substract(pkg_all, pkg_apt)

It shouldn't be hard to put together subtract. Apply a given function to all elements of pkg_all, another function to all elements of pkg_apt, in such a way that both lists are brought to a common notation. Then sort both lists, and diff.

This simply adds to the accepted answer.

Related:

  1. How do I detect and remove Python packages installed via pip?