How to make 'python' program command execute Python 3?
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
[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.
On Ubuntu 20.04+ you can do
sudo apt install python-is-python3
On top of that, you can prevent Python 2 from being installed as a dependency of something in the future with apt-mark hold
:
sudo apt-mark hold python2 python2-minimal python2.7 python2.7-minimal libpython2-stdlib libpython2.7-minimal libpython2.7-stdlib