Apt Package Management Python – Restore Python Installation to Default Plus Apt Package Dependencies

aptpackage-managementpippython

I have played around a lot with my Python installations (2 and 3) and installed a bunch of packages, some using apt-get, but most using pip. I have also upgraded many apt-installed packages with pip to a newer version.

My question is:
How can I reverse all those changes, remove all pip-installed Python packages and just keep the default set of preinstalled packages in their default repository version, plus those installed by apt-get but only as dependency of other software, always downgrading to the repository version if necessary.

I want to do this to have a tidied up Python environment for the system and have my modifications in virtualenvs only.

Is that possible without a system reinstall? How would I approach it?

Best Answer

I would start by listing the Python packages managed by apt-get (the following one-liner takes a few seconds to run, be patient):

$ for pyfile in `ls /usr/lib/python2.7/dist-packages`; do dpkg -S "/usr/lib/python2.7/dist-packages/${pyfile}" | sed 's/:.*//g'; done | sort -u

Note: if necessary, replace 2.7 with whatever python version you're interested in.

Next, uninstall the packages you installed manually. If you are unsure which ones you installed, you can get the list of all packages installed manually using the following command (see this question):

$ comm -23 <(apt-mark showmanual | sort -u) <(gzip -dc /var/log/installer/initial-status.gz | sed -n 's/^Package: //p' | sort -u)

Then uninstall the Python packages you installed manually, for example:

$ sudo apt-get remove python-tk python-scipy

Next, you want to know which packages you installed using pip:

$ ls -ltd /usr/local/lib/python2.7/dist-packages/*

This command lists the contents of pip's install directory sorted by last modification date, making it easier to find the packages you installed yourself. For example, I installed Google's gcloud tool which installs a bunch of pip packages that I don't want to uninstall, but looking through the list it's easy to see that they were all installed at the same exact time, so I know which ones I should leave, and which ones I can uninstall. Before you uninstall any pip package, you should take a snapshot of the list of packages installed:

$ sudo pip freeze > $HOME/pip_freeze_snapshot.txt

Then just uninstall the packages you want, for example:

$ sudo pip uninstall py pytest

Finally, don't forget that some pip packages may have been installed in your user directory. Perhaps you're fine with that, but if not, you can list them:

$ ls -ltd $HOME/.local/lib/python2.7/site-packages/*

If you want to get rid of all these packages you can just delete this directory. Otherwise, just uninstall the individual packages, for example:

$ pip uninstall tensorflow

Don't forget to check for other python versions (e.g., 3.5).

Related Question