24

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
3
  • 1
    You can type python3 instead python, so in script you can type: #!/usr/bin/env python3
    – Benny
    Aug 26 '16 at 20:00
  • 2
    yes, but I'm using scripts not made by me. I prefer not to change them.
    – roeland
    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
20

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

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
2

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.

1
  • This isn't true anymore
    – Neil G
    Jan 11 '20 at 10:58
2

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.

1

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

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.