Reputation: 6285
In python the first line of the script should be
#!/usr/bin/env/python{3}
Is there a way to do:
if python:
#!/usr/bin/env/python
else:
#!/usr/bin/env/python3
EDIT:
Running python
in RHEL7 bring up python-2.7.5 (default).
Only running python3
will execute python3-3.6.8 on my RHEL7.
Upvotes: 1
Views: 200
Reputation: 189913
None of those are correct. The correct command is /usr/bin/env
and the argument to that, after a space, is the name of the actual interpreter you want it to look up.
Your question is a bit of a "turtles all the way down" problem. You have to know what command you want to run. You could of course create yet another tool and call it something like py3orpy
but then you need to know that that exists in the PATH
before you try to ask env
to find it.
I would expect us to eventually converge on python
; if you can't rely on that, perhaps the least error-prone solution is to figure out what's correct for the target system at installation time, and have the installer write a correct shebang. Or just install a symlink /usr/local/bin/python3
if it doesn't exist already, and hardcode #!/usr/bin/env python3
everywhere.
Upvotes: 1
Reputation: 1990
I can't say I recommend this at all, but it does work:
#!/bin/bash
_=""""
if [[ -x '/usr/bin/python3' ]]
then
exec /usr/bin/python3 "$0" "$@"
elif [[ -x '/usr/bin/python' ]]
then
exec /usr/bin/python "$0" "$@"
else
echo "No python available"
exit 1
fi
"""
import sys
sys.stdout.write("you made it\n")
Can't do from __future__ import print_function
because it has to be the first line of python.
Principles here are to use bash
to do the executable detection but also make it valid python so you can just re-execute with the python interpreter on the same file.
Upvotes: 1