I need to write a Python script that's compatible with both Python 2 and Python 3, can be called directly from a shell (meaning it has a shebang), and can run on systems that have either Python version installed, but possibly not both.
Normally I can write a shebang for Python 2 like so:
#!/usr/bin/env python
and for Python 3:
#!/usr/bin/env python3
But both of these will fail if their corresponding Python version is not installed, since as far as I'm aware, systems that have Python 3 but not Python 2 do not alias or symlink python to python3. (Do correct me if I'm wrong.)
So is there a way to write a Python script with a shebang that will execute the script using either Python 2 or Python 3 so long as one of them is installed?
This is mainly intended to solve the removal of Python 2 from upcoming releases of macOS, (Note: I originally wrote that under the mistaken understanding that Python 2 would be replaced with Python 3 in macOS, but in reality Apple is removing Python completely.) but can apply to Linux as well.