How to remove all traces of python from 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.
Related videos on Youtube
Rnj
Updated on September 18, 2022Comments
-
Rnj almost 2 years
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?-
Raffa over 3 yearsDon't ... Ubuntu will not function without python.
-
Artur Meinild over 3 yearsYou never want to remove python completely - this may break your installation - see here.
-
Rnj over 3 yearsthen 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?
-
Raffa over 3 yearsTry this if you need a fix. Chances are little though. If you manage to fix, you are lucky.
-
Rnj over 3 yearsSo basically no simple way to get back to clean python state?
-
Raffa over 3 yearsIt'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.
-
user535733 over 3 yearsYou 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
anddpkg
, 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. -
marcelm over 3 yearsDid you use
pip
to globally install Python packages in/usr
? If so, that was not a good idea; now those files are all mixed up with Ubuntu's Python files. Cleaning that up is tricky. By the way, it's/usr/lib
, not\usr\lib
. -
Rnj over 3 yearsIt seems that
pip
itself doesnt come preinstalled in ubuntu. And when I didsudo apt install pip
and thenpip list
, it showed huge list of packages installed. Usually on windows, when you install python and then dopip list
, you have only two packages insntalled, setuptools and wheel. Also I installed only pipenv withsudo
as suggested by this answer, since with non sudo installation, running pipenv was giving command not found error. So i believe some packages need to be installed withsudo
to put them in/usr
, right?
-
Levente over 3 yearsCould 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.
-
Raffa over 3 years@Levente That is a good idea. Doing it now.
-
Mast over 3 years"Golden rule... leave the snake alone." sigh Python has nothing to do with snakes.
-
Raffa over 3 years@Mast No need to sigh, it's two snakes the more the merrier. Smile :)
-
OrangeDog over 3 yearsAmong the dependants are
ubuntu-minimal
, the network dispatcher, the update system, the snap system, and the firewall manager. Removingubuntu-minimal
marks things likesudo
as unneeded. -
Eric Duminil over 3 yearsNot 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 over 3 yearsFor 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
orpython3
were nowhere to be found. -
Nobody over 3 yearsThis might solve OP's problem, but won't do what they asked for (because they asked for the impossible). Should mention that.
-
peterh over 3 years@EricDruminil Probably some NetworkManager thing went away. My Ubuntu VMs (all server) work find without python.
-
peterh over 3 years@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 over 3 years@EricDuminil Too broad. Which could be worst than python, that would be narrower. I once get their list with a python script, but only if it will be able to convert an ascii7 string to utf8.
-
Eric Duminil over 3 years@peterh-ReinstateMonica your answer would be good without the rant in the beginning. Of all the languages I know (Java, python, c++, ruby, JS), python has the least amount of WTFs, while JS, the best language designed in 10 days, has so many WTFs everywhere that it's not funny anymore. Still, part of your answer is technically correct, so I didn't downvote.
-
Eric Duminil over 3 years@Nobody: I'm not sure I understood. Why do you consider what OP asked to be impossible?
-
peterh over 3 years@EricDuminil My experience is the opposite. In about 2008, I had more than one, long and big Python projects. Then I learned to hate Python for my life. Now there is 2021, I have with python again much to do, and... it is exactly the same.
-
Eric Duminil over 3 years@peterh-ReinstateMonica there are large, frustrating, bloated and buggy projects in every language. It doesn't mean that the language design or implementation are at fault, though.
-
peterh over 3 years@EricDuminil Only in Python are these the overwhelming majority. Try odoo or django, and enjoy them.
-
Nobody over 3 years@EricDuminil Ubuntu ships with Python by default and will break in many, many places without it (see many, many other comments pointing that out).
-
Eric Duminil over 3 years@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 over 3 years@peterh-ReinstateMonica Okay, you appear to be somehow biased against Python. There are many excellent python libraries, the language itself if easy to write, and more importantly, easy to read (almost boringly so). Not every library is good indeed, and some APIs are all over the place (e.g. matplotlib) but the language is getting better every month and it's incredible what one can achieve with a few lines of Python. In comparison, JS has many weird, fundamental flaws and its design is quite simply broken.
1+'2'
vs1-'2'
is just one of many repulsive warts. -
peterh over 3 years@EricDuminil Yes I am biased by my experience. I remember certbot which can not follow lsb, putting whole certificate histories below /etc and needing regular, manual fixes of his self-crapped config files. I remember node-gyp, making nodejs in 2021 (!!!) dependant on python 2. I remember mach (builder tool of Firefox), making the firefox practically closed source. I remember ufw which can not remove its own iptables rules on stop. I remember the infinite long of coding exceptions, even in python3. Yes, I have put them together and identified the root of the problems: Python.
-
peterh over 3 years@EricDuminil I have no much better opinion from js, too - however, odoo, the worst open source software I've seen in my whole life, has nearly killed a more than a year long project already in about 2010. From this about 1 yr, about 10 months were spent to fix it and about 2 months went to development. A very big part of both the python and JS "wtf"-es are caused by the typelessness of the languages, which is in my opinion, an essentially bad conception. A well-working language must be strongly typed, and support encapsulation (private variables/methods).
-
Eric Duminil over 3 years@peterh-ReinstateMonica: Good thing that Python is strongly typed and supports encapsulation, then. JS is, in contrast, very weakly typed.
-
peterh over 3 years@EricDuminil It is "dynamically typed", meaning that you can change the type of a variable while it exists, causing the worst bugs each after the other. And it does not support encapsulation - there is a convention, that methods/variables starting with
_
are considered private - unfortunately, all the python libs and softwares I've seen, regularly violate it, on two reasons: 1) the libs are very low quality, there is no other way to deal with them than using their private variables 2) also the end products are VLQ, it is no problem for their devs to use private members. -
Eric Duminil over 3 years@peterh-ReinstateMonica: Yes, it's dynamically typed, and strongly typed. stackoverflow.com/a/11328980/6419007 . If you want, you can also add type hints: docs.python.org/3/library/typing.html
-
peterh over 3 years@EricDuminil "dynamically typed" means essentially weakly typed in this case, because it has the same disadvantage than the js: for the illusion of the flexibility, it only puts compile time errors into runtime. Honestly, I find a little bit annoying your hairsplitting. The really important question is not that python is s--t or not, because it is. The question is that is it an acceptable compromise. The details are too long for this comment, but my opinion is a bitter yes. I admit that a world without python would be overall worse than with it.
-
Eric Duminil over 3 years@peterh-ReinstateMonica: Yes, let's stop here. There's not much point in discussing further if you don't want to understand the difference between dynamic/static and strong/weak typing.
-
peterh over 3 years@EricDuminil You do not want to understand that putting compile time problems into runtime is not flexbility, but a source of problems hitting back everywhere. I do not want to understand the social mechanisms making this into the top of the open source world. You also do not want to understand that yes, I admitted that overall a world without python would be worse, although this admittance is bitter.
-
Eric Duminil over 3 years@peterh-ReinstateMonica Yes, dynamic typing can hide problems, and it's a trade-off between conciseness, efficiency, ease of refactoring, safety... But at least Python complains when you're trying to add
1
and"2"
, because it's strongly typed. JS will almost never throw an exception, and is happy to return something as long as it returns anything. It's weakly typed, so it's fine to add arrays, numbers and strings. It does not even put compile errors into runtime errors, it puts them into weird results. I'll try to keep an eye for the design flaws you mentioned for Python libs. -
Rnj over 3 yearsI resolved the issue for which I was thinking (incorrectly) the solution will be to remove all python traces from Ubuntu. However, now I want to have really simple way to fix my Ubuntu from which I deleted those python folders. I know you gave a link, but really not having confidence that it will succeed. I wish there was something like recover / restore / repair Windows, may be using installation disk. This shouldnt be impossible to have with Ubuntu, right? Or may be Ubuntu devs might not have found repair worth to implement when we can always reinstall?
-
Raffa over 3 years@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 withaptitude
. There is no easy way unfortunately but, it's doable. This guy did it . -
peterh over 3 years@EricDuminil I am sorry but dynamical types have exactly the same problem. And, most importantly, that it has no encapsulation (private members), only a custom that "_" are considered private, and this custom is continuously violated by both the libs and the end apps (which is because both the libs and the end apps are crap). About at the early 2000s, the case was the same with php: huge mass of beginner programmers, and terrible quality of the available code base. Since then, php has developed a little bit, but its structural problems remained. Python does imho the same.
-
peterh over 3 years@EricDuminil Another problem of python is the incompatibility, the crap of the python2, the regular exceptions with string handling, the surreal problems with multi-line strings, such braindamages like that integers between 0 and 255 are fixed objects while larger integers are generated runtime, and a lot. Virtually if you see anywhere in the python world, you can find only this crap. Everywhere! Everywhere!
-
peterh over 3 years@EricDuminil Yeah, and yet another thing: reference counting garbage collector, lack of multi-thread support (extended with continuous <strike>lies</strike> "optimizations" in the terminology where they call processes threads, increasing the confusion by trying to hide the disadvantages). Crap, crap, crap. I can not see a single thing for which I could say, it was done well. Not a single one.
-
peterh over 3 years@EricDuminil Try to make a working solution with odoo. Now, more than a decade later, my teeth are clattering if I think back, what a "software" is it.
-
Eric Duminil over 3 years@peterh-ReinstateMonica: No thanks, you convinced me to not even try odoo. I can easily believe that some Python projects are really bad, and odoo probably tries to do too much. Some of your other points might be valid, but python2 is basically gone, multiline strings work fine. I don't care much about the implementation behind integers, as long as they're fast enough and can be arbitrarily large. It's pretty cool to be able to calculate
11**12345 + 1
without precision loss and without any lib. Numpy, pandas, sympy, pvlib, networkx, pygame, keras, astropy are all cool and just do the job. -
peterh over 2 years@EricDuminil Well, python has improved a lot in the last decade. And recently also containerisation can help a lot to circumvent its problems. I am considering the removal or significant edit of the answer. I think, today the largest problem around python is the low probability that you will use the same framework in consecutive projects.