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 venv (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 | |

I'm on Win10. I used this code of example. The file name of scripts is 'interpreter.py' The venv folder with the interpreter is 'text' The script is in 'C:\venv'

Launched it with cdm with this command C:> C:\venv\interpreter.py


#! "C:\venv\text\Scripts\python.exe"
import sys
import time
import torch
print('\nprint name_Py')
| improve this answer | |
New contributor
Marco Graziano is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • 1
    This is an Ubuntu site, please don't post answers for Windows here. – Mark Kirby 5 hours ago

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
| improve this answer | |
New contributor
deWalker is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.