Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python 3.12 shim script fails as a shebang line when called from a bash shell #57

Closed
gregneagle opened this issue Feb 21, 2024 · 8 comments

Comments

@gregneagle
Copy link
Contributor

Python scripts using #!/usr/local/bin/managed_python3 may fail to run as expected when called from a bash shell.
The following uses the "python_recommended_signed-3.12.1.80740.pkg" packaged release:

bash-3.2$ /usr/local/bin/managed_python3 -V
Python 3.12.1

bash-3.2$ cat test.py
#!/usr/local/bin/managed_python3
print("Hello, world!")

bash-3.2$ ./test.py 
./test.py: line 2: syntax error near unexpected token `"Hello, world!"'
./test.py: line 2: `print("Hello, world!")'

I'm guessing this same issue occurs with the 3.11 package as well.

(Same script works fine when called from a zsh shell:)

bash-3.2$ zsh
% ./test.py
Hello, world!
@erikng
Copy link
Member

erikng commented Feb 21, 2024

It's because the shim contains a shebang for zsh. How do you think we should handle this?

@erikng
Copy link
Member

erikng commented Feb 21, 2024

Also see

#55 and gregneagle/relocatable-python#31

@gregneagle
Copy link
Contributor Author

No, the fact the shebang contains zsh is not the cause here. The behavior is the same if the shebang is #!/bin/sh

@gregneagle
Copy link
Contributor Author

I think the only real approaches at this point are to either replace the shim with a small compiled binary, or remove the shim and symlink entirely and recommend people use #!/Library/ManagedFrameworks/Python/Python3.framework/Versions/Current/bin/python3 as the shebang.

Maybe, just maybe, we can get python.org to fix the upstream issues. Ideally, they would build the Python.framework in such a way that it could be relocated without us having to hack at it with the relocatable-python tool. Barring that, perhaps we could get them to at least fix the issue where it can't find the platform-specific libraries when the binary is called via a symlink.

@notverypc
Copy link

Is there anything us MacAdmins can do to help get python.org to fix the upstream issues?

@gregneagle
Copy link
Contributor Author

FIle issues?

@gregneagle
Copy link
Contributor Author

I think I have stumbled across a much better workaround for the Python 3.11+ issues. Instead of symlinking to Python.framework/Versions/Current/bin/python3, you make the symlink to Python.framework/Versions/Current/Resources/Python.app/Contents/MacOS/Python

This avoids the "Could not find platform dependent libraries <exec_prefix>" and does not require a shim script/executable.

@erikng
Copy link
Member

erikng commented Feb 26, 2024

Closing this as the latest pre releases contain this fix and initial testing looks fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants