11

I have a third party script that uses the shebang #!/usr/bin/env python.

According to the python documentation, this is the correct form for scripts that are suitable for both Python v2 and Python v3 (https://docs.python.org/3/using/windows.html#shebang-lines).

My Ubuntu (WSL) has only Python 3 installed:

~❯ which python
~❯ which python3
/usr/bin/python3

I have added this to the path, by adding the following in my ~/.bashrc and sourcing it

export PATH=/usr/bin/python3:${PATH}
export PY_PYTHON=3  # also added this for good measure, but no joy
~❯ . ~/.bashrc
~❯ env | grep PATH
PATH=/home/me/.local/bin:/home/me/bin:/usr/bin/python3:/opt/gradle/gradle-5.3.1/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Yet when I run this script it complains that it cannot find Python. I have created a short test file foo that contains the same shebang and a print("Hello"):

~❯ ./foo
/usr/bin/env: ‘python’: No such file or directory

What else can I do to make env use python3?

PS: I do not have python 2, nor do I wish to install it. The third party script's README specifically mentions supporting both Python v2 and v3, and the shebang indicates this, as mentioned, according to the Python docs themselves.

PPS: Adding aliases does not resolve this as they are only effective when the user is calling python at the prompt, not when the bash itself is running a script. This answer to a similar question explicitly cites this behaviour: https://askubuntu.com/a/351380/333103

PPPS: The received wisdom from other questions is that symlinking is not the correct solution to env issues (don't have link to hand just ATM)

3
  • 1
    Change the shebang to #!/usr/bin/python3 – WinEunuuchs2Unix Nov 16 '19 at 18:19
  • 1
    @WinEunuuchs2Unix It's a set of third party script. I could change it for this particular set of script, but I'd have to change all of them each time it gets updated. The Python docs indicate that #!/usr/bin/env python should work – Toby Nov 16 '19 at 18:23
  • 1
    Fair enough. Since we are talking about paths the program env searches, can you update your question with echo $PATH? – WinEunuuchs2Unix Nov 16 '19 at 19:10
7

Since OP does not install python2 and doesn't need /usr/bin/python as python2, I'm suprised that no one mentioned the most simple solution.

You just symbolic link python to python3 with sudo ln -s python3 /usr/bin/python, and it will work.

Or, config the alternatives as mentioned in the comment(3.6 if you're using Ubuntu 18.04 on WSL):

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 100
4
  • Usually packages creates these links, an update may re-link python again – bac0n Nov 18 '19 at 8:29
  • Yes, but up to today, when some package really needs python2 and install python2, it will not accept python3 as /usr/bin/python either. If you want python3 as default python then it's up to the user to deal with it. You cannot have /usr/bin/python as python2 and python3 at the same time. – Alvin Liang Nov 18 '19 at 8:36
  • 3
    Probably update-alternatives --config python is better way to create these symlinks - reference. – pa4080 Nov 18 '19 at 8:40
  • Excellent, the update-alternatives looks like the solution I need here :) – Toby Nov 18 '19 at 11:53
8

env cannot find python because env is searching python in your PATH, not on any shell builtin, or alias or function. As you have defined python as using EXPORT env won't find it, it will search through 'PATH' and will resolve python to /usr/bin/python (which is python2 and not present in the system).

You can check all the available locations of executable python, in bash, do:

type -a python

You are unlucky 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
3
  • 1
    AFAIK this aliases the command at the bash prompt - not the env usage. I can also say I have tried this and it makes zero difference :( – Toby Nov 16 '19 at 18:08
  • As mentioned, it's a 3rd party script, I do not control its contents – Toby Nov 16 '19 at 18:25
  • How will env resolve python to /usr/bin/python? That location does not exist on my PATH or my file system... as I mentioned Python 2 is not installed. Or maybe this is some kind of default somehow? – Toby Nov 16 '19 at 18:26

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.