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
  • You can type python3 instead python, so in script you can type: #!/usr/bin/env python3 – Benny Aug 26 '16 at 20:00
  • 1
    yes, but I'm using scripts not made by me. I prefer not to change them. – r03 Aug 26 '16 at 20:01
  • 4
    Python2 and python3 are not compatible. Those scripts probably expect python 2 – Hack Saw Aug 26 '16 at 20:04

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
| improve this answer | |
  • 2
    Thanks. I made it work like this for now: 'ln -s /usr/bin/python3 ~/bin/python'. ~/bin is first in the PATH. – r03 Aug 26 '16 at 20:21
  • 2
    @r03 Don't do that, look at virtualenv instead. – heemayl Aug 26 '16 at 20:29

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.

| improve this answer | |
  • This isn't true anymore – Neil G Jan 11 at 10:58

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.


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.

| improve this answer | |

Without disturbing the whole system, IMHO, the best approach is to create a virtual env in your use account and activate it

/path/to/your/python3 -m pip create ~/.venv

source ~/.venv/bin/activate

Then, your default python will be the one you have used to create the Vence (in the path, etc)

With that solution, you’ll also don’t disturb the system when installing new libs, and won’t need root access.

Other solutions might impact stability of your OS

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.