710

The python program command executes Python 2. Python 3 can be executed using the python3 command. How can Python 3 be executed using the python command?

Giri
  • 7,243
  • 3
  • 13
  • 9
  • 47
    Just a warning: Do not attempt to change the /usr/bin/python symlink to point to python3 instead of 2.7. Many programs available in the Ubuntu repos require /usr/bin/python to be compatible to python 2.x. – soulsource Jul 17 '13 at 08:17
  • @soulsource, yeah I'm aware of that. that's why i asked is there an "upgrade" option available or not. – Giri Jul 17 '13 at 08:24
  • I think the answer by Radu Rădeanu is already quite close to the optimal solution. It only applies to one user, and is only in effect for directly running python typing the python command, not affecting programs with a #!/usr/bin/python shebang. – soulsource Jul 17 '13 at 08:44
  • 1
    Ah, now I got what you meant with upgrade... Actually the Ubuntu developers are working on that: https://wiki.ubuntu.com/Python/3 "It is a release goal for Ubuntu 14.04 LTS to have only Python 3 on the desktop CD images." – soulsource Jul 17 '13 at 08:45
  • @soulsource that's what I'm searching for! :) thanks! – Giri Jul 17 '13 at 09:36
  • 2
    On another note, anyone coming here because they are trying to learn about making Python 3 their default, may instead find use in researching virtual environments (e.g. virtualenv) or containers (e.g. LXC or Docker). – Kevin Sep 13 '16 at 19:38
  • 1
    In reference to soulsource’s warning at the top see PEP 394 which standardises naming conventions for coexisting Python executables and on which Python programmers and package maintainers do (and should) rely. – David Foerster Jun 25 '18 at 11:26
  • @Kevin I just tried using a virtualenv, but as soon as I opened Gedit from the terminal, I got a bunch of errors cause its plugins seem to need Python 2. Similar problems could happen for any other software that relies on the command python being Python 2. – wjandrea Dec 04 '18 at 19:03
  • 2
    @wjandrea, yes even in a virtual environment, python should be kept as meaning python2. I meant my comment as a way to have an application specific Python version instead of trying to work around the system's Python. – Kevin Dec 05 '18 at 18:57
  • If you are one user running python programs inside your home, a safe and conservative solution is to add alias python='echo Python2 is too old. Please run python3' to your .bashrc – user334639 May 30 '19 at 16:31
  • 1
    sudo apt install python-is-python3 (for Ubuntu 20.04+) –  Apr 28 '20 at 18:49

7 Answers7

787

You can install a system-wide package:

$ sudo apt install python-is-python3

See caveats: python-is-python3 package in Ubuntu 20.04 - what is it and what does it actually do?

A simple safe way would be to use an alias. Place this into ~/.bashrc or ~/.bash_aliases file:

alias python=python3

After adding the above in the file, run source ~/.bashrc or source ~/.bash_aliases.

For example:

$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3

To circumvent the alias use the command built-in command:

$ command python --version
Python 2.7.6

Another way to circumvent the alias is to use \ before the command.

$ \python --version 
Python 2.7.6

To disable the alias in the current shell use the unalias built-in command:

$ unalias python
$ python --version
Python 2.7.6
Radu Rădeanu
  • 169,590
  • 1
    so no "upgrade" option..?? – Giri Jul 17 '13 at 08:14
  • @Giri You said that you have python3. What for an upgrade? – Radu Rădeanu Jul 17 '13 at 08:16
  • 4
    actually i want to wipe-out 2.7 and replace it with 3.3. Seems like its a bad idea for now.. – Giri Jul 17 '13 at 08:26
  • 52
    +1 there is no reason to purge 2.7 in order to be able to work with 3.3. As lots of software still depends on 2.7; just keep it lingering around. – don.joey Jul 17 '13 at 09:39
  • is not alias = symbolic link ? I ask this because I read that it is not a good thing to set a default python version this way. –  May 28 '14 at 12:48
  • 11
    @begueradj An alias is totally different from a symbolic link. – Radu Rădeanu Jun 03 '14 at 07:41
  • 3
    While this will allow you to run Python3, it doesn't allow you to install packages from source. 'python setup.py build' will build python3 code but 'python setup.py install' will put them in your python2 site-packages directory. – timbo May 20 '15 at 04:16
  • Wouldn't it be better to alias python2 to python, and develop a habit of specifying which you intend to use? – ryanjdillon Oct 29 '15 at 09:43
  • 1
    You would have to do the same for every python command, including pip, pylint, pytest, etc. Creating a virtual environment would be a better solution. – rtaft Aug 10 '17 at 17:36
  • 1
    One more way (not totally recommended) to achieve that is change your symbolic link (sl): cd /usr/bin, then sudo rm python to remove the sl and sudo ln -s python3.6 python (check your version before). You still can use python2.7 if you need. This approach is not recommended because other packages dependencies, but I have been using it and I hadn't any problem for a long time. Use it for your own risk. – sdlins Apr 20 '18 at 03:06
  • @wjandrea, was not it that I said ? – sdlins Jul 02 '18 at 19:13
  • My CRON still using my older Python2 version – Umair Ayub Jul 03 '18 at 09:27
  • 5
    A word of caution: this may break other scripts that expect Python 2.7. – kmiklas Oct 29 '18 at 18:46
  • @don.joey hi,how to make my terminal permanently will execute py3 code instead of default py2 ? – Infinite Loops Nov 19 '18 at 15:59
  • @InfiniteLoops that's actually what this answer explains. use an alias. – don.joey Nov 21 '18 at 03:08
  • Just for anyone who wants easy access, I did this. Idk if it is necessary, but it's safe https://pastebin.com/f3jaLKxs – user189728 Dec 28 '18 at 07:42
  • 13
    Using alias python='python3' does not appear to work when calling sudo python. In this case, it opens the default python2.7 instead. – Mitchell van Zuylen Feb 21 '19 at 11:00
  • 1
    This doesn't work for programs that call eg. /usr/bin/env python in the shebang. – jkflying Nov 12 '19 at 23:14
  • @kmiklas This won't break scripts since aliases don't affect scripts. The shebang for example is run by the kernel. – wjandrea Dec 10 '19 at 18:17
  • @jkflying Yes, you'll still need to explicitly specify the Python version in the shebang, e.g. #!/usr/bin/env python3 since the shebang is run by the kernel which is unaffected by aliases. – wjandrea Dec 10 '19 at 18:18
  • This doesn't work for me in a Dockerfile context. – Dan Nissenbaum Apr 27 '20 at 22:09
  • 1
    I have added alias python=python3 to .bashrc... Is it possibe to add another alias to make pip3 the default? – Hooman Bahreini Jul 15 '20 at 19:46
  • Just building openvino on Ubuntu 20.04 and the sudo make install command expects there to be python as the sudo user. The accepted answer sudo apt install python-is-python3 solves this where an alias doesn't. If you have old code that depends on python 2.7 I would probably try to run on 18.04 LTS instead. – brianlmerritt Oct 27 '21 at 18:20
  • 1
    @MitchellvanZuylen add a alias sudo='sudo '. Aliases are only applied to the command, which is the first word. In sudo python ./foo.py -i, the command is sudo and the arguments are ["python", "./foo.py", "-i"], where no alias is applied. You do not want that the command file python is executed as file python3. However, making sudo also an alias to sudo (space is important) makes bash checking for aliases on the first argument too. – 12431234123412341234123 Jan 05 '22 at 14:57
107

[June 2016] The recommended place for information on the transition is official Ubuntu Python page.


From the Ubuntu wiki:

For both Ubuntu and Debian, we have ongoing project goals to make Python 3 the default, preferred Python version in the distros.

What this does not mean:

  • /usr/bin/python will point to Python 3. No, this is not going to happen (unless PEP 394 advocates otherwise, which is doubtful for the foreseeable future). /usr/bin/python and /usr/bin/python2 will point to Python 2.7 and /usr/bin/python3 will point to the latest supported Python 3 version.

  • Python 2 will be removed from the archive. No, this is not going to happen. We expect Python 2.7 to remain supported and available in Ubuntu for quite a long time, given that PEP 373 promises upstream bug fix maintenance support until 2020.

It is not recommended to change the symbolic link because of other package dependencies, but they "have ongoing project goals to make Python 3 the default, preferred Python version in the distros".


For CLI use, like @Radu Rădeanu, I would recommend putting an alias in the user's ~/.bashrc, .bash_aliases file (the different files, including ~/.bash_profile, are loaded at least once, are mostly for organizational purposes, but may vary by platform). Python virtual environments also work well.

Alias examples:

alias python=python3

or

alias python='/usr/bin/python3'

Scripts should still use something like #!/usr/bin/env python3 for cross-compatibility.

Using env is nice for mixed use with virtual environments.

Note (thanks to @wjandrea): aliases are part of the bash runtime, not the user environment. Therefore, they are not available to the shebang (#!). If you prefer the alias python=python3, then some program.py without a shebang could be executed by invoking the aliased interpreter like this python program.py. Aliasing may also be useful for systems with multiple version of python3 like 3.4 and 3.6 together.

Kevin
  • 1,315
  • 2
    This seems to be out of date now: they planned to remove python2 in Xenial, but then didn't. – OrangeDog Jun 20 '16 at 11:30
  • 1
    @OrangeDog, thanks for the update. Yes, the wiki-page I cited is now flagged as out-of-date, as more progress has been made in the past two years for moving to only Python 3. The new page to follow this progression is the official Ubuntu Python page. – Kevin Jun 20 '16 at 13:27
  • The "latest" page is also out of date, regarding the plans for Xenial. – OrangeDog Jun 20 '16 at 13:42
  • I would not say it is "out of date" so much as "it has not been updated recently." It still seems to be the current location for keeping posts about this. However, in the comments here, I would appreciate any other recent sources anyone may find. Adding more sources to my answer, about why not to make the change to just python, is not really relevant until the conversion is complete. Even then, it may just become a non-issue. – Kevin Jun 20 '16 at 13:51
  • @OrangeDog it is very much possible to have a Xenial system without Python 2 - I've run Ubuntu Xenial Mate on Raspberry PI and it had desktop but no python2.7. There are still some optional packages (or ones needed for desktop experience) programs that pull Python 2 in so chances are you're having those. – Antti Haapala Apr 16 '17 at 07:52
  • Progress tracking for Fedora: https://fedoraproject.org/wiki/FinalizingFedoraSwitchtoPython3 and https://fedoraproject.org/wiki/Changes/Python_3_as_Default – Kevin Aug 24 '18 at 00:18
  • 3
    Aliases are internal to Bash, not part of the environment, so you will still need to use python3 in a shebang, not python. – wjandrea Sep 28 '18 at 22:51
  • @wjandrea, oh wait, are you saying that #!/usr/bin/env python with alias python=python3 will not execute Python3? Let me double-check this. – Kevin Sep 29 '18 at 12:23
  • @Kevin Yes, that's exactly what I'm saying. – wjandrea Sep 29 '18 at 16:06
57

Update: This is the wrong way, I have learned, since Python2 and Python3 are not interchangeable.

You can try the command line tool update-alternatives.

$ sudo update-alternatives --config python

If you get the error "no alternatives for python" then set up an alternative yourself with the following command:

$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10

Change the path /usr/bin/python3 to your desired python version accordingly.

webwurst
  • 2,385
  • 27
    python2 and python3 are not alternatives. Do not use update-alternatives for this purpose. – jobin Jun 02 '14 at 18:37
  • 1
    Why aren't they? Can one of you please explain why update-alternatives is not suitable for python? Is it because of http://legacy.python.org/dev/peps/pep-0394/ ? – Dmitry Grigoryev Feb 19 '16 at 14:28
  • 18
    alternatives are different implementations for the same functionalities. python2 and python3 do not provide the same functionalities. – Ely Dec 13 '16 at 19:01
  • 1
    You can do something like this if you manually installed (via ppa or whatever) other versions of python3. sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2 – David Baucum Aug 20 '18 at 14:45
  • This is what I needed to do for an 18.04.2 vm that does not come with python. Thanks – Dark Star1 Feb 20 '19 at 12:05
  • How can I revert this? – Vishnudev Krishnadas Oct 20 '19 at 15:04
  • "python2 and python3 do not provide the same functionalities" but they do, python2 --help python3 --help and you will see the same functionalities. update-alternatives is also used to change between php cli versions, so changing between python versions should also be acceptable. sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 30; sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 20 – pmiguelpinto90 Sep 08 '21 at 09:24
  • this IMO the best option.. while trying to install and run aws-cli there is no other way to make python3 available as python which the cli uses – Gautam Feb 13 '22 at 14:50
14

Ubuntu, and the rest of the Linux distros for that matter, are still largely dependent on Python 2.7 for a number of applications and commands. If you change the default reference of "python" to Python 3.x, then a number of Python functions will start throwing assertion errors.

For example, on Ubuntu, 'pip' for one would no longer run correctly unless you directly edited the file and changed the shebang to reference '#!/usr/bin/env python2.7'. On RHEL (Red Hat Enterprise Linux) flavors such as Red Hat, Fedora and CentOS, the 'Yum' command is also dependent on Python 2.7.

My point here is that you would cause a significant amount of code to start throwing assertion errors just so you could type 'python' in the terminal to reference Python 3.x.

You're much better off with using the 'python3' command in the terminal and the shebang '#!/usr/bin/env python3' in your Python 3.x files.

Moony
  • 141
  • 1
  • 3
6

Do

cd ~
gedit .bash_aliases

then write either

alias python=python3

or

alias python='/usr/bin/python3'

Save the file, close the terminal and open it again.
Link

Hossein
  • 1,680
3

I find it very helpful to simply remove /usr/bin/python and /usr/bin/pip. This forces all programs to rely on the "python2" and "python3" commands.

Although some optional and outdated packages depend on #!/usr/bin/python to work, I would rather submit patches to those programs than continue to make weird and sometimes hard-to-debug mistakes.

Erik Aronesty
  • 359
  • 3
  • 6
-1
cat > /usr/local/bin/py << 'EOF'
#!/bin/dash
python3 "$@"
EOF

(provided you have write permission to /usr/local/bin) likewise

cat > /usr/local/bin/pyi << 'EOF'
#!/bin/dash
python3 -i "$@"
EOF

then you only type py (and use py in #! lines) for your chosen python.

wjandrea
  • 14,236
  • 4
  • 48
  • 98