Default to python3 for '/usr/bin/env python'
I installed python2.7 and pyhton3.5 with apt-get.
In my .bashrc I have alias python=python3
to make python3 the default.
This works if I just run python directly, but it doesn't seem to work with /usr/bin/env.
How can I force python3 in this case?
$ python --version
Python 3.5.2
$ /usr/bin/env python --version
Python 2.7.12
Solution 1:
That's because env
is searching python
in your PATH
, not on any shell builtin, or alias or function. As you have defined python
as python3
as an alias, env
won't find it, it will search through PATH
and will resolve python
to /usr/bin/python
(which is python2
).
You can check all the available locations of executable python
, in bash
, do:
type -a python
You are out of luck if you want to use an alias in shebang as by definition, shebang needs to be an full path to the interpreter executable, which the env
should resolve python
to when you use /usr/bin/env python
. To interpret the script using python3
use the shebang:
#!/usr/bin/env python3
Solution 2:
It's 2020, and things have changed:
From the Focal Fossa Release Notes:
Python3 by default
In 20.04 LTS, the python included in the base system is Python 3.8. Python 2.7 has been moved to universe and is not included by default in any new installs.
Remaining packages in Ubuntu which require Python 2.7 have been updated to use /usr/bin/python2 as their interpreter, and /usr/bin/python is not present by default on any new installs. etc, etc
Unfortunately, as I have learned, this change in the Ubuntu 20.04LTS distro may not have been done as well as it could. Some applications are apparently not on board with this change, and just won't work without some help.
If you need Python2, you don't have much choice except to install it, and live with the chaos. If you don't need it, there are at least two system-wide solutions that may make your life easier:
1. Install python-is-python3
:
$ sudo apt-get install python-is-python3
2. Apply update-alternatives
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
update-alternatives: using /usr/bin/python3 to provide /usr/bin/python (python) in auto mode
Solution 3:
Given the number of script which call /usr/bin/env python expecting python 2, it's probably a bad idea to have python actually be python 3.
As Benny said in a comment, /usr/bin/env python3
is the right solution.
Solution 4:
I found a better solution than those posted here: http://redsymbol.net/articles/env-and-python-scripts-version/
The basic idea is to put a symlink name python to python3 in some other smartly named directory and then put that directory in the beginning of $PATH so it gets found before the one at /usr/bin.
So:
mkdir ~/bin/env_python3/
ln -s /usr/bin/python3 ~/bin/env_python3/python
$PATH = ~/bin/env_python3/:$PATH ./script.py
Using this solution you don't symlink /usr/bin/python to python3 and break scripts that assume it is python 2 and you also don't have to edit the script that you downloaded from someone else.