How do you set the "python" command to point to python2 instead of python3?

Viewed 4469

Due to my own stupidity, I somehow set the python command to run python 3 instead of the default python 2. How do I undo this? I am on Ubuntu 18.04.

$ python -V
Python 3.7.6
$ python3 -V
Python 3.7.6
5 Answers

You can change the alias python="/usr/bin/python2.7",for example :

alias python="/usr/bin/python3.3"
python
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 16 2013, 23:39:35) 

You can also add this line in your .bashrc file

echo "alias python=/usr/bin/python3.3" >> ~/.bashrc 

You can list all the version of Python installed and change the default Python version Try the following commands on terminal.

$:~# update-alternatives --config python
There are 4 choices for the alternative python (providing /usr/local/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
  0            /usr/bin/python3     4         auto mode
  1            /usr/bin/python2.7   2         manual mode
* 2            /usr/bin/python3     4         manual mode
  3            /usr/bin/python3.7   3         manual mode
  4            /usr/bin/python3.8   4         manual mode

Press <enter> to keep the current choice[*], or type selection number: 4
update-alternatives: using /usr/bin/python3.8 to provide /usr/local/bin/python (python) in manual mode

Set the default python version using python version. Finally, you can check the success of command as below.

root@kali:~# python --version
Python 3.8.2

It should show Python 2.x in your case.

On most Unix-Linux systems, python is a link or a symbolic link in /usr/bin or /usr/local/bin. You should search where it is with type python and control what it is.

If it is a link or a symlink, just remove it and make it point to your installation of python 2.

Use type python to check what it points to, then check your .bashrc for two things:

  • whether the path from type python is somewhere included in PATH
  • whether you defined an alias for python somewhere

To install python2, run:

sudo apt install python-minimal

To run python2,

python2

Related