Default to python3 for '/usr/bin/env python'
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
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.
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.