60

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.

4
  • 3
    You said you installed both python & python3 ? when they were both already installed (ie. Lubuntu 18.04 LTS comes with both though 3.6 for python3). How did you install them? and did you remove or replace the pre-installed versions? I suspect your issue is $PATH, that you removed the preinstalled versions that were in your path, installed it elsewhere or amended your $PATH to something different to default.
    – guiverc
    May 19, 2019 at 8:11
  • i installed python 3.7
    – Boni
    May 19, 2019 at 8:21
  • I asked HOW you installed them. If you used commands; history can refresh your memory on how. On my Lubuntu a whereis python tells me where it is, and the command echo $PATH shows the list of directories searched for commands. What I suggested may be corrupted on yours, or if you installed from source (why I asked HOW) it could be installed outside of $PATH defaults. If I enter python --version mine responds 2.7.16, and python3 --version responds 3.7.3 (note: I'm not using 18.04 so it's likely later versions). Did you remove anything? and how was it installed?
    – guiverc
    May 19, 2019 at 12:22
  • While people have pointed to various ways to make python invoke Python 3, is there a particular reason you can't just run python3? If there is, it might be worth explaining what that reason is. Feb 19, 2021 at 21:58

5 Answers 5

57

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.

5
  • 4
    It's not a good idea to make direct changes to /usr/bin/ under the feet of the operating system. Doing so may cause problems on future upgrades. Better approaches would be installing python-is-python3 (only on 20.04 or newer), using a per-user alias python=python3, or simply running python3 directly. Feb 19, 2021 at 21:59
  • The packaging system assumes that it owns /usr/bin/ (and /usr/ in general outside of /usr/local/) and that users do not write there directly. Arbitrary things may go wrong if you violate this rule, even if it seems relatively harmless. Feb 22, 2021 at 0:51
  • It's not at all clear whether that's true. The OP said they installed Python 2.7 and 3.7, but Ubuntu 18.04 comes with 2.7 and 3.6, so they must be doing something odd. It's possible they have Python installed in some non-standard way rather than via the packaging system, so maybe they don't actually have the python package installed. Feb 23, 2021 at 10:40
  • @ColinWatson is right, it's safer to use an alias rather than a symlink because other system-wide package dependencies may still require python2. More here May 19, 2021 at 7:21
  • thanks that worked. Just need to relaunch terminal after adding alias, otherwise it won't work.
    – Liker777
    Aug 7, 2022 at 14:41
43

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.

10
  • 4
    This is good advice for 20.04, but the OP asked about 18.04, where python-is-python3 doesn't exist and where the rest of the OS is not prepared for /usr/bin/python being Python 3 and may fail unpredictably if that's forced. Feb 19, 2021 at 22:01
  • 1
    Gosh, thanks Colin, I didn't check the package existance in 18.x sorry, I have modified the answer by specifing that applies only to 20.x, sometimes this is useful cause SO users fall here looking to similar issues. But if you think that it is better... I'll remove the answer. Feb 23, 2021 at 9:32
  • 2
    Seems reasonable to just carefully qualify it as you've done. Thanks. Feb 23, 2021 at 10:41
  • 1
    @jspinella There is a big difference. alias command is temporary and valid only for the session it has been issued in, and only for interactive commands (ie. in a script python won't be recognized as a command). python-is-python3 installs an actual /usr/bin/python which is a symlink to /usr/bin/python3, so python becomes permanently equal to python3 in virtually all contexts. So they are completely different.
    – raj
    Oct 27, 2021 at 19:02
  • 1
    So alias is temporary and python-is-python3 installs a symlink to permanently point python to python3. python-is-python3 does not install python3 and assumes python exists at usr/bin/python3
    – jspinella
    Oct 27, 2021 at 19:57
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.

1

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
1
  • 1
    "you want the default python to be python2, for compatibility reasons" -- Thanks for pointing this out. I completely hosed a previous Ubuntu installation (<20.01) by trying to be forward-thinking in ditching python2 by aliasing python as python3, and never understood why everything stopped working. Now I see where I went wrong.
    – Ivan
    Jul 27, 2022 at 19:12
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.

1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .