Ubuntu – Default to python3 for ‘/usr/bin/env python’

bashcommand linepython

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

Best Answer

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