24

I have successfully installed python 3.3 on Ubuntu 12.10. Since I don’t need multiple versions of python, I want to remove the existing python 2.7. When I try to do that, using

sudo apt-get remove python2.7

Ubuntu warns me that there are tons of system dependent components which will also be removed. It looks really scary.

So, is there a way to remove python 2.7 without removing the system dependent components, or can I direct those dependents to use python 3.3?

TRiG
  • 1,910
  • 2
    I would not recommend it. Python version are not at all so compatible, so older scripts could have experience problems if you will not have the correct version. – tikend Jul 01 '13 at 13:44
  • you are right, python3.3 has declared that it is incompatible with python 2.7. So, I better just keep it. Thanks for the advice! – phoenix bai Jul 01 '13 at 13:49

3 Answers3

29

You can't.

From the Ubuntu wiki / Python:

Longer term plans (e.g. 14.04)

Move Python 2 to universe, port all Python applications in main to Python 3. We will never fully get rid of Python 2.7, but since there will also never be a Python 2.8, and Python 2.7 will be nearly 4 years old by the time of the 14.04 LTS release, it is time to relegate Python 2 to universe.

This means that a lot of base packages have hard dependencies on 2.7 and it will still take a lot of time tot get things migrated. Note that Python 3 has numerous backwards incompatible changes -- it's not a regular package upgrade.

If you really want to get rid of Python 2.7, you'll have to wait for the 14.04 release, but there's no guarantee.

Taymon
  • 743
gertvdijk
  • 67,947
15

Came here in 2019 because I develop in Python3 by default and came to the same conclusion as OP after seeing what'd be removed after running apt purge python

Since what I really wanted was to call Python3 with just python, I ran

sudo rm /usr/bin/python
sudo ln -s /usr/bin/python3 /usr/bin/python

This way, if Python2.7 is still needed, it can be called explicitly with python2.7 while simply calling python will default to Python3 because of the symbolic link.

I don't have any bash level scripts that call python2.7 with python so this change wouldn't be disruptive - while other systems would need their scripts adjusted accordingly if they did.

The main barrier to a distribution switching the python command from python2 to python3 isn't breakage within the distribution, but instead breakage of private third party scripts developed by sysadmins and other users.

- The "python" Command On Unix-Like Systems

This answer isn't a direct response to OP, but as someone who had a similar question this is the functionality I was looking for when I was thinking of removing 2.7. Rather than delete, just prioritize which one gets to use python.

sanigirl
  • 291
  • 1
    It strikes me as a bad idea. Any old scripts made for Python 2 will call /usr/bin/python, whilst almost every script made for Python 3 will be aware of the change, and call python3 at the present time. Letting python point to python2.7 is less likely to break things. – vidarlo May 29 '19 at 21:59
  • I'd say it depends on the environment, like I said I don't make scripts that require python2 and work in 3 - while if I made a change like this on one of our stations at work running automated scripts it would be very disruptive. – sanigirl May 29 '19 at 22:04
  • Are there specific system processes you would be able to list that call /usr/bin/python? I'm always open to changing my mind when presented with new information – sanigirl May 29 '19 at 22:07
  • I find quite a few in 18.04. grep -r "/usr/bin/python" /usr/bin/ should give you a brief overview. It will probably not disable your system, but it may make some stuff not work. For instance solaar. – vidarlo May 29 '19 at 22:11
  • i'm on 18.04.02 and the only ones I see that call /usr/bin/python and not explicitly 2.7/3 are pip, dh_python2, and pip2. I can see a change like this being disruptive in a different environment that has a lot of python2 dependent scripts, would you say it's more about technical debt? I'm curious about whether this is more of a gray area or if it's more clear as to why exactly it's a bad idea – sanigirl May 29 '19 at 22:18
  • 1
    On the system I checked, there's 39 that does not make a explicit reference to python3 or python2. All of those are probably python2. This of course depends on what you have installed, but I see no big reason to do this either... – vidarlo May 29 '19 at 22:21
  • Fair enough. Thanks for the extra perspective! 3 python2 related programs vs 39 making non-explicit calls to python is a pretty big deal so I can accept this as Bad Advice. – sanigirl May 29 '19 at 22:23
  • It may very well be acceptable to some users, depending on what packages they have installed. It simply breaks some expectations... :) – vidarlo May 29 '19 at 22:24
  • I did what you mentioned but now a lot of things are going haywire. Please just tell me how to undo what I did. I want my system to call python 2.7 if I run python and 3.8 if I run python3 just like before. Please tell me how to revert it back. – nkhl Jul 31 '20 at 18:34
  • 1
    Remove the symbolic link and reinstall the python binary – sanigirl Jul 31 '20 at 18:52
  • I am sorry but could please tell me the command to do this. I don't know much about shell commands. – nkhl Aug 01 '20 at 02:32
7

You can't, and you don't really want to.

Python changed drastically between 2.7 and 3.0, and broke backward compatibility. Python scripts that were written for 2.7, which are used to support a large amount of the system's infrastructure, won't necessarily work properly with Python 3.x. Those scripts need to be updated to work with the new version, and until that happens, you'll need to keep Python 2.7 around.

This is why you notice such a large number of dependencies on the old Python - the system depends on it. Besides, there's no harm in having both versions of Python installed on the same system. And you may come across applications in the future that still use Python 2.7, so keeping it around is a good idea.

user173076
  • 71
  • 1