1

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.

5
  • What do you mean by In python the first line of the script should be #!/usr/bin/env/python{3}? As far as I'm concerned there is nothing like that. Commented Apr 15, 2021 at 19:03
  • @IsmailHafeez, to indicate it is python script, not a Perl one or a shell one. Commented Apr 15, 2021 at 19:05
  • @Igor The first line of a python script should not be that. That's not a python thing. That's just a bash/whatever other unix shell thing. It tells the shell what interpreter to use, it's called a shebang (short for hash # bang !). It's only a thing for unix/linux/etc. systems, nothing to do with python at all. Commented Apr 15, 2021 at 19:09
  • @RocketHazmat, but my question is not about that. It is about dynamically switching between python and python3 where people has to work with both on 2 different environments. Commented Apr 15, 2021 at 19:12
  • @Igor You should probably use a library like six for compatibility between python2 and python3. See: six.readthedocs.io Commented Apr 15, 2021 at 19:12

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

13 Comments

we are working under RHEL7 and will soon go to RHEL8, where python3 is the default. However we are also working under VM where python3 is not yet available. Is there a way to dynamically identify what should be run?
If you control the environment, of course you can. If you can force every VM to have python3, there you have it.
@Igor You should probably use a library like six for compatibility between python2 and python3.
@tripleee, I wish. But it is not up to me. I can fix my VM, but not the one generated. Besides, we have to maintain compatibiliyu.
@RocketHazmat, not, both scenarios are different - see edit to the OP.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.