Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Add python to usr/bin/env for shebang to work

I'm trying to use a python script which uses this shebang:

#!/usr/bin/env python


Problem is as i've understood macOS Monterey isn't supporting this anymore. So how do get this line to call python?


FYI: I've already added python to the path but

/usr/bin/env python --version

results in:

env: python: No such file or directory


Thanks for your help



MacBook Air 13″, macOS 12.4

Posted on Jul 26, 2022 4:06 AM

Reply
Question marked as Best reply

Posted on Jul 26, 2022 4:44 AM

Starting with macOS 12.3.1, Apple removed the Python 2.7.18 distribution from the operating system. So, no reference to python will launch anything since /usr/bin/python no longer exists.


One of your options would be to install Python 2.7.18 (deprecated) from python.org which is X86_64 binary only that installs the following:


/Library/Frameworks/Python.framework/Versions/2.7


and binaries from this distribution are linked into /usr/local/bin as:


python python2 python2.7 idle idle2 and idle2.7


Then, provided you had altered your startup file {.bash_profile/.zshrc) to:


export PATH=".:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:$PATH"


then your #!/usr/bin/env python would work as expected.


It might be better to forget about Python 2.7.18 and just install python 3.10.5 (Universal2 binary) or later from Python.org as it is currently supported and newer than the Python3 that Apple includes with Xcode or just the Command Line Tools for Xcode. It would install into the same locations as above with the exception of being named python3, idle3, etc. Changes would be:


export PATH=".:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:$PATH"


#!/usr/bin/env python3


and any Python2 to Python3 specific changes to your code that may be necessary. The Python.org code does not directly support Apple's Python scripting bridge that was lost with the removal of Python 2.7.18, so you won't be doing any Python/Objective-C programming without tweaks.


Python.org download page for either of the above Python releases. This is a .pkg installer designed for macOS.



1 reply
Question marked as Best reply

Jul 26, 2022 4:44 AM in response to StephanF1403

Starting with macOS 12.3.1, Apple removed the Python 2.7.18 distribution from the operating system. So, no reference to python will launch anything since /usr/bin/python no longer exists.


One of your options would be to install Python 2.7.18 (deprecated) from python.org which is X86_64 binary only that installs the following:


/Library/Frameworks/Python.framework/Versions/2.7


and binaries from this distribution are linked into /usr/local/bin as:


python python2 python2.7 idle idle2 and idle2.7


Then, provided you had altered your startup file {.bash_profile/.zshrc) to:


export PATH=".:/Library/Frameworks/Python.framework/Versions/2.7/bin:/usr/local/bin:$PATH"


then your #!/usr/bin/env python would work as expected.


It might be better to forget about Python 2.7.18 and just install python 3.10.5 (Universal2 binary) or later from Python.org as it is currently supported and newer than the Python3 that Apple includes with Xcode or just the Command Line Tools for Xcode. It would install into the same locations as above with the exception of being named python3, idle3, etc. Changes would be:


export PATH=".:/Library/Frameworks/Python.framework/Versions/3.10/bin:/usr/local/bin:$PATH"


#!/usr/bin/env python3


and any Python2 to Python3 specific changes to your code that may be necessary. The Python.org code does not directly support Apple's Python scripting bridge that was lost with the removal of Python 2.7.18, so you won't be doing any Python/Objective-C programming without tweaks.


Python.org download page for either of the above Python releases. This is a .pkg installer designed for macOS.



Add python to usr/bin/env for shebang to work

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.