1

Must the shebang header always match the interpreter's installation directory? If so, then why do both #!/usr/bin/python and #!/usr/local/bin/python work for me?

3
  • I think that it is more common to install python in /usr/bin/ than in /usr/local/bin/. As Guardian said in case of your distribution it may be symlink. In my Gentoo second option will fail, as there is no python and no symlink. Also you could check my answer about recommended shebang for python...
    – pbm
    Feb 27 '13 at 21:30
  • 1
    See also this question and my answer to it. Feb 28 '13 at 1:55
  • The both work because both /usr/bin/python and /usr/local/bin/python are valid interpreters on your system. They may or may not be the same. Feb 28 '13 at 1:56
7

As others have noted, the shebang line must refer to an actual file that exists.

Since different systems can have binaries installed to different locations, this is a weakness of script portability.

One way to solve this is with links, as others have mentioned.

Another way is to edit the script to refer to the right path on your system.

One more way to get around this is to use /usr/bin/env, which will find the executable in your PATH.

So instead of:

#!/usr/bin/python

Write:

#!/usr/bin/env python

This way, people just have to symlink /usr/bin/env if needed, rather than every individual executable.

Of course, the executable still has to be in your PATH for this to work...

As you can see, there is no 100% clean solution. Remember: "Worse is Better" (:

1
5

The #! (shebang) line must specify an executable that actually exists. If both work on your system, it means you have a python installed in both places. Or, possibly, a symlink.

Normally, you'd get an error attempted to use an interpreter that doesn't exist:

anthony@Zia:~$ /tmp/test 
bash: /tmp/test: /usr/local/bin/python: bad interpreter: No such file or directory

If you instead run the script like this, then the path likely doesn't matter:

anthony@Zia:~$ python /tmp/test
anthony@Zia:~$ 

That's because you're running python directly, and passing it an argument. Python then decides to open that file and treat it as a script. In the first case, the kernel is attempting to run the script as an executable. It checks it, notices shebang line, and attempts to convert it to the second form, so it can actually run it. That fails if the path given after #! doesn't actually exist.

3

Shebang is a combination of the following characters: #! when it appears in the first line of the script. The following string is always the path to the interpreter. I'd check what exactly /usr/bin/python and /usr/local/bin/python are. It's possible that one of them is a symlink into the other. Could you please, provide the output of the following commands:

  • ls -l /usr/bin/python
  • ls -l /usr/local/bin/python
1
  • avimehenwal@AviMehenwal-DYNAC:~/Documents/PYTHON$ python p1.py Hello World avimehenwal@AviMehenwal-DYNAC:~/Documents/PYTHON$ python p2.py Hello World avimehenwal@AviMehenwal-DYNAC:~/Documents/PYTHON$ ls -l /usr/bin/python lrwxrwxrwx 1 root root 9 Feb 24 23:27 /usr/bin/python -> python2.7 avimehenwal@AviMehenwal-DYNAC:~/Documents/PYTHON$ ls -l /usr/local/bin/python ls: cannot access /usr/local/bin/python: No such file or directory Feb 27 '13 at 21:32

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.