On my Ubuntu system, I have a lot of python libraries manually installed in /usr/local/lib by pip a long time ago, some of which are now outdated by the ones from the official ubuntu packages in /usr/lib. This can be the source of problems since the ones in /usr/local/lib have precedence in the default python environment.

What do you think would be the best way to identify from the /usr/local/lib tree the libraries for which a newer version exists in /usr/lib and the ones that are now longer needed ?

I was thinking of monitoring the /usr/local/lib/python* trees for python scripts accessing its libraries and manual checking if the corresponding library in /usr/lib/python* works but is it the best strategy ?

1 Answer 1


Unfortunately, there's no way to check that with existing command line tools right now as listing packages with pip is pretty limited (I guess more will be possible once #4575 is implemented). However, with a little scripting, you can perform the package filtering and version comparison yourself. Here's a proposal:

#!/usr/bin/env python3

from distutils.version import LooseVersion
from pkg_resources import find_distributions

sys_pkgs = find_distributions('/usr/lib/python3/dist-packages')
local_pkgs = find_distributions('/usr/local/lib/python3.6/dist-packages')

for pkg in local_pkgs:
    sys_pkg = next((p for p in sys_pkgs if p.project_name == pkg.project_name), None)
    if sys_pkg is not None and LooseVersion(sys_pkg.version) >= LooseVersion(pkg.version):

First, the script scans /usr/lib/python3/dist-packages and /usr/local/lib/python3.6/dist-packages for installed packages (adjust the Python version/target paths depending on your needs). After that, for each "local" package check that:

  1. a "system" package with the same name is installed and
  2. has a newer version than the "local" one

If both conditions are met, the "local" package is considered obsolete and will be printed. Once you have the list, uninstalling its entries via usual pip3 uninstall <pkgname> should be a straightforward task.

  • Glad I could help!
    – hoefling
    Nov 30, 2018 at 12:46

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.