How to remove all traces of python from Ubuntu

I want to clean all traces of python from my Ubuntu. Is there any easy solution?

To start with I guess I should remove all pip packages. I tried command as suggested here, but got bunch of failure messages:

#pip3 freeze | xargs pip3 uninstall -y
Found existing installation: appdirs 1.4.4
Uninstalling appdirs-1.4.4:
  Successfully uninstalled appdirs-1.4.4
Found existing installation: attrs 19.3.0
Not uninstalling attrs at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'attrs'. No files were found to uninstall.
Found existing installation: Automat 0.8.0
Not uninstalling automat at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'Automat'. No files were found to uninstall.
...

What should I do? This answer asks to run:

sudo rm -rf /usr/local/lib/python2.7/dist-packages/twitter

Should I run? I have py files at following paths:

  • \usr\lib\python2.7
  • \usr\lib\python3\dist-packages
  • \usr\lib\python3.8

Also this unaccepted answer asks to do:

sudo apt remove python-numpy

I am currently on wsl2 Ubuntu. And am wary, because today only, I (possibly) screwed my another Ubuntu installation, by accidentally deleting all above pythonXYZ folders. Now I am neither able to remove all traces of python nor able to reinstall python on that machine. It keeps giving me some error (may be I have to ask separate question for that). But how do I fix this WSL Ubuntu?


Solution 1:

Please don't.

Ubuntu relies heavily on different Python versions for functionality. New releases of Ubuntu are slowly shifting to Python3, but older versions of Python are still in use.

You can list some important Ubuntu and Gnome packages on your system that depend on Python3, for example, like so:

apt-cache rdepends -i --installed --recurse python3 | \
grep -v " " | sort -u | grep -E "ubuntu|gnome"

On Ubuntu 20.10 desktop, these important packages are among them:

gnome-control-center
gnome-session
gnome-terminal
network-manager-gnome
ubuntu-desktop
ubuntu-desktop-minimal
ubuntu-drivers-common
ubuntu-minimal
ubuntu-release-upgrader-core
ubuntu-release-upgrader-gtk
ubuntu-session
ubuntu-standard
ubuntu-system-service

Moreover, there is no such Python clean state. Each system update and each package you install might bring with it Python related dependencies.

You can however use pip or pip3 to uninstall only packages you previously manually installed and even this is not totally risk free.

If you have already removed Python, try this or this if you need a fix. Chances are little though. If you manage to fix it, you are lucky.

Golden rule... Leave the snake alone.


That being said, use a Python virtual environment for your Python projects and you shouldn't be needing to clean or go back to clean state Ubuntu system Python.

Python virtual environments create an isolated environment for your Python projects. This means that each project can have its own dependencies, regardless of what dependencies the Ubuntu system or other Python projects have.

This feature can be installed for Python3 like so:

sudo apt install python3-venv

To make a Python3 virtual environment for a project, you would first create a directory and cd to it like so:

mkdir my_env && cd my_env

Then, create a new Python3 virtual environment inside the directory like so:

python3 -m venv env

This will create a structure like this:

$tree -L 3

.
└── env
    ├── bin
    │   ├── activate
    │   ├── activate.csh
    │   ├── activate.fish
    │   ├── Activate.ps1
    │   ├── easy_install
    │   ├── easy_install-3.8
    │   ├── pip
    │   ├── pip3
    │   ├── pip3.8
    │   ├── python -> python3
    │   └── python3 -> /usr/bin/python3
    ├── include
    ├── lib
    │   └── python3.8
    ├── lib64 -> lib
    ├── pyvenv.cfg
    └── share
        └── python-wheels

To use this environment, activate it like so:

source env/bin/activate

Your shell prompt will show (env) like so:

(env) $

During this, Python3 commands, module installs or modifications will be contained locally in this virtual environment.

When you are done, deactivate this Python3 virtual environment like so:

deactivate

You are now back to the system-wide Python3 and commands will take effect globally so be careful.

Solution 2:

Here's a method:

get 'apt-cache' to show reverse-dependencies, recursively, of the core python library; "--installed" to limit to packages installed, and "-i" to show only important dependencies (i.e. not suggests or recommends).

The 'grep' filters out all except package names, then sorted uniquely (there'll be many duplicates), then use 'xargs' to append the resulting list of lines as parameters to 'apt-mark auto', which marks them as automatically installed.

'Automatically installed' packages will be removed by 'apt autoremove' when no more packages depend on them.

apt-cache --installed  -i --recurse rdepends \
  libpython3.8-minimal | \
    grep "^  " | sort -u | \
      xargs apt-mark auto

apt autoremove

This will show the long list of packages to be removed, be careful of unexpected dependencies removing packages you want to keep!

Say 'no' to that prompt and 'apt-mark manual ThisOne' for all the packages you need to keep, and run 'apt autoremove' again (and check again!) to get rid of the junk.