How to reset OS:X installation of Python packages installed using sudo?

Question:

Imagine that one did not realize how virtual environments work in Python and installed a lot of packages using sudo pip install for OS:X. Now they are facing problems managing package versions.

This would never happen if one understands virtual environments, but if one did this prior to being enlightened, how can that someone easily remove all my their non-virtual environment packages that were installed, without breaking any default installations?

Note this includes several programs (such as nosetests) and is not limited to exclusively libraries. It seems I can create a virtualenvironment with --no-site-packages and that gets around at least the packages (assuming I wipe my PYTHONPATH). But my actual PATH seems to let me see the executables I installed, too.

Asked By: enderland

||

Answers:

Nothing in my base installation of OS:X is installed with pip. This means you can uninstall everything from pip on your OSX without “worrying” – in terms of actual system performance. It is possible this will interfere with your day-to-day activities, if you are relying on global pip packages.

You can verify what packages you have installed by viewing the full list:

pip freeze

Everything this returns are user installations. This means you can “safely” pass this as arguments to pip uninstall:

pip freeze | xargs sudo pip uninstall -y

which will uninstall all items installed with pip on your machine.

You may accidentally be using these in some of your virtual environments, particularly if your PYTHONPATH variable is set to any of your local install directories. Any of pip’s installed packages that are executables will also be visible inside of a virtual environment, assuming that you are not overwriting your PATH variable as part of your virtual environment.

In my case, the only item I had to reinstall was the virutalenv wrapper:

pip install virtualenvwrapper
Answered By: enderland

There may be a case when you installed packages to python without sudo and willing to clear the base python installation.
In this case, it can be done like this:

python -m pip freeze | xargs python -m pip uninstall -y

In case there are several python versions and you want to clean them all, just the command for of them individually, substituting python with python3.x (x – desired version).

Answered By: MrCheatak
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.