5

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?

9
  • 19
    Don't ... Ubuntu will not function without python. – Raffa Mar 5 at 19:27
  • 4
    You never want to remove python completely - this may break your installation - see here. – Artur Meinild Mar 5 at 19:28
  • then what should I do if I want to at least reset it to clean state with which ubuntu ships? that is, at least remove all unnecessary (non-minimal) packages? – Rnj Mar 5 at 19:29
  • 2
    It's complicated and better be left alone. 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 to uninstall only packages you previously manually installed and even this is not totally risk free. Golden rule... leave the snake (python) alone. – Raffa Mar 5 at 19:44
  • 1
    You are essentially asking "I have thrown my bicycle over a cliff into the sea. How do I fix it?" You don't. In Windows, you reinstall WSL to restore your install. A skilled admin can reinstall Python3 using just wget and dpkg, but it's somewhat tedious (I've done it)...and seems like a waste of time in a VM environment like WSL. In A VM environment, you throw away a (guest) machine when you are done with it, and spin up a new (guest) machine anytime you need one. – user535733 Mar 5 at 19:57
27

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.

6
  • 3
    Could you please add a paragraph about a way that would enable someone to use one's own python projects in small self-contained "containers"? Which would allow for not polluting, or not interfering with Ubuntu's global installation scope? "Leave the snake alone" is a very good/educative starting point, but seems unsatisfactory when planning to work on python projects on the long term. – Levente Mar 5 at 21:11
  • @Levente That is a good idea. Doing it now. – Raffa Mar 5 at 22:42
  • "Golden rule... leave the snake alone." sigh Python has nothing to do with snakes. – Mast Mar 6 at 7:56
  • 3
    @Mast No need to sigh, it's two snakes the more the merrier. Smile :) – Raffa Mar 6 at 13:30
  • 2
    @Rnj It is not totally hopeless.The link I provided in the answer might work since the running system will be the one on the live DVD/USB and aptitude should take care of dependencies... give it a try... I know I can make it work, so you can. You can also download python3 minimal with its dependencies and install it with dpkg but, it's easier with aptitude. There is no easy way unfortunately but, it's doable. This guy did it . – Raffa Mar 8 at 18:48
3

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.

4
  • This might solve OP's problem, but won't do what they asked for (because they asked for the impossible). Should mention that. – Nobody Mar 6 at 21:47
  • @Nobody: I'm not sure I understood. Why do you consider what OP asked to be impossible? – Eric Duminil Mar 7 at 11:11
  • @EricDuminil Ubuntu ships with Python by default and will break in many, many places without it (see many, many other comments pointing that out). – Nobody Mar 7 at 14:31
  • 1
    @Nobody I just tried it. It was still kinda usable, even if it becomes closer to a debian server than to an Ubuntu desktop. Probably not a good idea but surely not impossible. – Eric Duminil Mar 7 at 14:34
-3

I happily agree your heretic opinion that the life is much better on a system without python as with it. The problem is not only with the language implementation, but also with the concepts of it (it is a basic script language with a surreal syntax, yet it is used for complex OO projects). And the most important problem is the very low quality of the available software base, both of the libraries and of the end products, to the extent that it endangers even the open source community et al. But it is only my opinion, others have different, sometimes opposite opinions. Here I only explain, how to purge python from your system for all.

Contrary the popular belief, you can purge python from your whole system - it is not an essential package, fortunately. Some tools and softwares depend on it, so it is possible that you will need them. In this case, a minimal python (if you have luck, at least not python2) needs to remain. Other packages (like node.js) depends on it no reason, also they should be purged (alternatively, a sane re-packaging project not depending on them would be useful, hopefully someone will once do it).

The package dependencies are a DAG: all packages have a list of packages in which is depends on, also these depend on other packages and so on.

What you need: map these dependencies and find the root of them. For that, you have three most important commands:

  • dpkg -s <package> shows, which packages depend on <package>, if it is installed on your system. apt-cache show <package> does the same for a package in your apt cache (packages available for installation in your registered apt repositories).
  • apt-cache rdepends <package> shows, which packages depend on <package>.
  • dpkg -S /usr/bin/python3.6 shows, which package has /usr/bin/python3.6. Note, /usr/bin/python is typically a symbolic link somewhere into /etc/alternatives, where there is a symlink to the real package binary, and these are not part of the registered file lists of the packages (available in /var/lib/dpkg/info/*.list). So possibly you will need to map the symlinks to their real version with ls -l commands and then use dpkg -S to find the package.

Using these, you can find the package on which all python package depends on. In the case of python, it is libpython2.7-minimal, or libpython3.6-minimal. You can list them by a dpkg -l|grep libpython.*minimal.

Then an apt --purge remove libpython2.7-minimal will purge (delete without any trace that they ever existed) python completely from your system. It will also list for you, exactly which packages will be removed, if you have some what you really need, unfortunately you will need to keep python.

The same can work if you want to purge other large software distributions (like X11 from servers and similars).

25
  • 4
    Among the dependants are ubuntu-minimal, the network dispatcher, the update system, the snap system, and the firewall manager. Removing ubuntu-minimal marks things like sudo as unneeded. – OrangeDog Mar 6 at 17:04
  • Not much would be left if you insist on remove everything dependent on python. Just curious: which language do you consider to be better designed than Python? – Eric Duminil Mar 6 at 20:13
  • For what it's worth: I just tried it in a VM, with Ubuntu 20.04 and libpython3.8-minimal. After a restart, the GUI was still working, but internet wasn't available anymore inside the VM, for example. python2 or python3 were nowhere to be found. – Eric Duminil Mar 6 at 20:24
  • @EricDruminil Probably some NetworkManager thing went away. My Ubuntu VMs (all server) work find without python. – peterh Mar 6 at 23:11
  • @OrangeDog Ubuntu-minimal is a virtual package, its existence does not effect anything. I don't know, what is a "network dispatcher", I did not met this terminology since 1995. There are a lot of firewalls, you don't need to use the Ubuntu builtin one (I don't even know it). – peterh Mar 6 at 23:15

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.