How to Update-Alternatives to Python 3 Without Breaking Apt

Unable to set default python version to python3 in ubuntu

EDIT:

I wrote this when I was young and naive, update-alternatives is the better way to do this. See @Pardhu's answer.


Outdated answer:

Open your .bashrc file nano ~/.bashrc. Type alias python=python3
on to a new line at the top of the file then save the file with ctrl+o
and close the file with ctrl+x. Then, back at your command line type
source ~/.bashrc. Now your alias should be permanent.

How to revert overriden python2 to python3 in Linux?

If you just messed up with the symlinks (/usr/bin/python2 and /usr/bin/python) and the actual executable is still in place, you might be able to fix the problem by restoring the links:

sudo ln -s /usr/bin/python2.7 /usr/bin/python2 
sudo ln -s /usr/bin/python2.7 /usr/bin/python

Otherwise you can try to download a live-ubuntu and just copy the Python executable from the live OS back in place to fix your broken installation.

How to set default python3 to python 3.9 instead of python 3.8 in Ubuntu 20.04 LTS

You should be able to use python3.9 -m pip install <package> to run pip with a specific python version, in this case 3.9.

The full docs on this are here: https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/

If you want python3 to point to python3.9 you could use the quick and dirty.

alias python3=python3.9

EDIT:

Tried to recreate your problem,

# which python3
/usr/bin/python3
# python3 --version
Python 3.8.10
# which python3.8
/usr/bin/python3.8
# which python3.9
/usr/bin/python3.9

Then update the alternatives, and set new priority:

# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 1
# sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
# sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

Selection Path Priority Status
------------------------------------------------------------
0 /usr/bin/python3.9 2 auto mode
1 /usr/bin/python3.8 2 manual mode
* 2 /usr/bin/python3.9 2 manual mode

Press <enter> to keep the current choice[*], or type selection number: 0

Check new version:

# ls -alith /usr/bin/python3
3338 lrwxrwxrwx 1 root root 25 Feb 8 14:33 /usr/bin/python3 -> /etc/alternatives/python3
# python3 -V
Python 3.9.5
# ls -alith /usr/bin/pip3
48482 -rwxr-xr-x 1 root root 367 Jul 13 2021 /usr/bin/pip3
# pip3 -V
pip 20.0.2 from /usr/lib/python3/dist-packages/pip (python 3.9)

Hope this helps (tried it in wsl2 Ubuntu 20.04 LTS)

Can I install python 3.7 in ubuntu 18.04 without having python 3.6 in the system?

I see two choices:

  1. Using a Ubuntu image, leave the Python from system untouched. Install pyenv (https://github.com/pyenv/pyenv), then download a python 3.7 install, completely separated from system's Python.

or


  1. Use the official Python image labeled 3.7.3-stretch or 3.7.3-slim-stretch (Debian)


Related Topics



Leave a reply



Submit