70

I have installed python 2.7 and python 3.7 in my ubuntu 18.04 but when i

type python it shows

  Command 'python' not found, but can be installed with:

  sudo apt install python3       
  sudo apt install python        
  sudo apt install python-minimal

  You also have python3 installed, you can run 'python3' instead.

but i already installed python.

Boni
  • 843

5 Answers5

64

As suggested in comments, you could create an alias as follows:

alias python='python3'

by adding it to the ~/.bashrc file at the end of this file, exiting and reloading it in the current terminal using the next command: . ~/.bashrc

Or using linking:

As you can see below, my python points to python2, python2 points to python2.7.

To achieve the same, use:

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

If you want python pointing to 3rd version, you could use the same, but the last command should be:

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

Example

$ whereis python
python: /usr/bin/python3.7 /usr/bin/python2.7-config /usr/bin/python2.7 /usr/bin/python3.7m-config /usr/bin/python3.7-config /usr/bin/python /usr/bin/python3.7m /usr/lib/python3.7 /usr/lib/python2.7 /usr/lib/python3.8 /etc/python3.7 /etc/python2.7 /etc/python /usr/local/lib/python3.7 /usr/local/lib/python2.7 /usr/include/python3.7 /usr/include/python2.7 /usr/include/python3.7m /usr/share/python /usr/share/man/man1/python.1.gz
user@lenovo:~$ ls -ailh /usr/bin/python*
1446954 lrwxrwxrwx 1 root root    7 жов 10 14:32 /usr/bin/python -> python2
1446952 lrwxrwxrwx 1 root root    9 жов 10 14:32 /usr/bin/python2 -> python2.7
1465834 -rwxr-xr-x 1 root root 3,6M лис  7 12:07 /usr/bin/python2.7
1447155 lrwxrwxrwx 1 root root   33 лис  7 12:07 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
1447156 lrwxrwxrwx 1 root root   16 жов 10 14:32 /usr/bin/python2-config -> python2.7-config
1442842 lrwxrwxrwx 1 root root    9 лют 12 00:23 /usr/bin/python3 -> python3.7
1449245 -rwxr-xr-x 2 root root 4,9M лис 20 11:21 /usr/bin/python3.7
1447339 lrwxrwxrwx 1 root root   33 лис 20 11:21 /usr/bin/python3.7-config -> x86_64-linux-gnu-python3.7-config
1449245 -rwxr-xr-x 2 root root 4,9M лис 20 11:21 /usr/bin/python3.7m
1447340 lrwxrwxrwx 1 root root   34 лис 20 11:21 /usr/bin/python3.7m-config -> x86_64-linux-gnu-python3.7m-config
1447341 lrwxrwxrwx 1 root root   16 жов  2 15:31 /usr/bin/python3-config -> python3.7-config
1442843 -rwxr-xr-x 1 root root  384 січ 30  2019 /usr/bin/python3-futurize
1442847 lrwxrwxrwx 1 root root   10 лют 12 00:23 /usr/bin/python3m -> python3.7m
1447342 lrwxrwxrwx 1 root root   17 жов  2 15:31 /usr/bin/python3m-config -> python3.7m-config
1442844 -rwxr-xr-x 1 root root  388 січ 30  2019 /usr/bin/python3-pasteurize
1447157 lrwxrwxrwx 1 root root   14 жов 10 14:32 /usr/bin/python-config -> python2-config
1455649 lrwxrwxrwx 1 root root   58 лип 10  2019 /usr/bin/pythontex -> ../share/texlive/texmf-dist/scripts/pythontex/pythontex.py
1450999 -rwxr-xr-x 1 root root  306 лип 10  2019 /usr/bin/pythontex3

For managing python3 versions, you can use python alternatives to create symbolic links:

$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.11 2

And choose which one to use as using the command:

$ sudo update-alternatives --config python3

For managing python2 and python3 using update-alternatives, you could see in michael's answer.

Gryu
  • 8,002
  • 9
  • 37
  • 53
51

This solution only applies to Ubuntu 20.04 (but sometimes people look at "similar issues" for a solution).

In the case you like python to refer to python3, you can simply install python-is-python3:

sudo apt-get install python-is-python3

After this, invoking python will work just fine.

4

I had same error

Ubuntu:/$ python

Command 'python' not found, but can be installed with:

sudo apt install python3       
sudo apt install python        
sudo apt install python-minimal

You also have python3 installed, you can run 'python3' instead.

you can try

Ubuntu:/$ python2.7
Python 2.7.17 (default, Nov  7 2019, 10:07:09) 
[GCC 7.4.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> 
>>> 

this How to make 'python' program command execute Python 3? would help to make python alias.

Poonam Adhav
  • 141
  • 3
2

Another option that would work across different (and older) versions of Ubuntu is the "alternatives" system, either the command-line update-alternatives (should be installed) or GUI galternatives (not installed by default). It lets one configure your "preferred" version when you have multiple versions, or completely different but compatible implementations, (e.g. compilers, editors; see for example this dump: update-alternatives --get-selections).

To see which versions of python are currently the default (note: these are symbolic links to specific versions):

$ type python python2 python3
python not found
python2 is /usr/bin/python2
python3 is /usr/bin/python3

Usually (like, always, without exception) you want the default python to be python2, for compatibility reasons (otherwise you will break apt and other system utilities!) and then just explicitly invoke python3 if and when you really mean Python3.

To see what current alternatives are configured for python:

$ sudo update-alternatives --list python
error: no alternatives found

If alternatives are found, skip the following (see --config, below). Otherwise, we can configure both python2 and python3 as "alternatives" for the python command and you can switch between them (or just set python2 here and you're done; the rest is here for completeness & as an exercise):

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 40
using /usr/bin/python2 to provide /usr/bin/python (python) in auto mode

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 20 using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode

$ type python /usr/bin/python

$ python --version Python 2.7.18

(Important! if your default python is showing as python3, then change it here to python2, otherwise apt and other system utilities may be broken!)

To use update-alternatives to change the version currently selected, do the following and choose the version you want:

$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

Selection Path Priority Status

  • 0 /usr/bin/python2 40 auto mode 1 /usr/bin/python2 40 manual mode 2 /usr/bin/python3 20 manual mode

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

To completely remove all python alternative configurations (e.g., to start over, or just undo everything if anything weird is going on):

$ sudo update-alternatives --remove-all python
michael
  • 2,109
0

Most times you've to use the keyword python3 to run the code other than just python on py, these won't work as expected.