Restore Python installation to default plus apt package dependencies

18,113

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

Share:
18,113

Related videos on Youtube

Byte Commander
Author by

Byte Commander

Ask Ubuntu moderator♦, IT student and DevOps engineer. I love Ubuntu, Python, good music and coffee, although not necessarily in that order. You can easily contact me in the Ask Ubuntu General Room most of the time, or on Discord as @ByteCommander#2800. I'd also love to invite you to my Ubuntu Hideout Discord Server btw. PS: My profile picture is derived from "Wolf Tribals" by user HaskDitex (DeviantArt.com), which is under creative Commons 3.0 License. Currently I'm using the character "Dregg Morriss" from the game "Medieval Cop" by Vasant Jahav ("Gemini Gamer"). It can be found here.

Updated on September 18, 2022

Comments

  • Byte Commander
    Byte Commander over 1 year

    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?

  • craq
    craq almost 5 years
    I'd recommend a public safety announcement that removing too many packages can cripple your system, but otherwise this is an excellent guide. (For example, I was glad I did mv rather than rm on my /usr/local/lib/python3.6 because I discovered a few minutes later that I needed some things in there for fairly basic system functions.