Must the shebang header always match the interpreter's installation directory? If so, then why do both
#!/usr/local/bin/python work for me?
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
So instead of:
This way, people just have to symlink
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" (:
Normally, you'd get an error attempted to use an interpreter that doesn't exist:
If you instead run the script like this, then the path likely doesn't matter:
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
Shebang is a combination of the following characters: