Selecting a Python interpreter using a shebang doesn't seem to work in some cases.
I'm trying to use my python3.9 interpreter in a shebang (#!/...
) on MacOS. See the Python-file below called hello.py
:
#!/nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/bin/python3.9
print("Hello world")
Now, I'll make sure this file is executable, and try to execute it:
$ chmod +x ./hello.py
$ ./hello.py
./hello.py: line 2: syntax error near unexpected token `"Hello world"'
./hello.py: line 2: `print("Hello world")'
That's weird. Did I mistype something? Let me try to execute it with my interpreter directly to see if it works:
$ /nix/store/i46k148mi830riq4wxh49ki8qmq0731k-python3-3.9.2-env/bin/python3.9 ./hello.py
Hello world
That seems to work fine. Why did the shebang fail? Let me try with a different interpreter:
#!/usr/bin/python3
print("Hello world")
$ ./hello.py
Hello world
And now it works? That's interesting.
Questions:
- Why does the /nix/store-path not seem to work in the shebang?
- Are there some limitations on shebang expressions on MacOS that I'm not aware of? For example a maximum length, or certain characters like
.
or-
that are not permitted?
My system:
- MacOS Big Sur 11.2.1 (20D74)
bash --version
: GNU bash, version 4.4.23(1)-release (x86_64-apple-darwin17.7.0)
Edit: Executing ./hello.py
from zsh instead of bash actually works (when using the nix-shebang). So that might hint at this being an issue specific to bash.
execve()
call ought to do when that call fails. That logic has a quite long history of bugs; it's not unbelievable to me that this would be one of them./usr/bin/python -c 'import subprocess; subprocess.call(["./test.py"], shell=False)'
, giving an Exec format error. This implies to me that the OS does have a problem with that shebang, and thus that we're relying on fallback behavior of the active shell.