What shebang to use for Python scripts run under a pyenv environment
Some interesting links to look at and when you need them, you need them:
- Python shebang has a nice explanation of the shebang in general as well
- shebang line for Python 2.7
- []
- what shebang to use for python scripts run under a pyenv virtualenv - it has an interesting take/riff on one of the answers that I never would have thought about. ephemient stated it first and then Neil McGill expounded on it more (with comments):
#!/bin/sh
#
#### Choose the python we need. Explanation:
#### a) '''\' translates to \ in shell, and starts a python multi-line string
#### b) "" strings are treated as string concat by python, shell ignores them
#### c) "true" command ignores its arguments
#### c) exit before the ending ''' so the shell reads no further
#### d) reset set docstrings to ignore the multiline comment code
#
"true" '''\'
PREFERRED_PYTHON=/Library/Frameworks/Python.framework/Versions/2.7/bin/python
ALTERNATIVE_PYTHON=/Library/Frameworks/Python.framework/Versions/3.6/bin/python3
FALLBACK_PYTHON=python3
if [ -x $PREFERRED_PYTHON ]; then
echo Using preferred python $ALTERNATIVE_PYTHON
exec $PREFERRED_PYTHON "$0" "$@"
elif [ -x $ALTERNATIVE_PYTHON ]; then
echo Using alternative python $ALTERNATIVE_PYTHON
exec $ALTERNATIVE_PYTHON "$0" "$@"
else
echo Using fallback python $FALLBACK_PYTHON
exec python3 "$0" "$@"
fi
exit 127
'''
__doc__ = """What this file does"""
print(__doc__)
import platform
print(platform.python_version())