I have a script that I am running on multiple Linux OSs. The shebang line of my script is:
#!/usr/bin/python
When I came across CentOS 8, this doesn't work as I get the following: "-bash: ./sysinfo-snapshot.py: /usr/bin/python: bad interpreter: No such file or directory"
And as a workaround I can change the shebang line to
#!/usr/bin/python3
and this would 100% solve the issue.
However, what I'm looking for is to have my script try the first shebang line, and if it didn't work, to try the second line, as in, 2 first lines in the code would be:
#!/usr/bin/python
#!/usr/bin/python3
Is this possible? Is there a way to support both python and python3 for the script? As in first try the first shebang line, if it didn't work try th second line.
Things I tried to far:
I tried changing the shebang line to: #!/usr/bin/env python
But it didn't work.
Thanks
You could just not have a shebang and execute your script with the appropriate python binary; e.g.
$ python3 yourscript.py
or
$ python yourscript.py
And you don't need executable permission for your script.
So to make it portable, use a wrapper script:
#!/bin/bash
if [ -x /usr/bin/python3 ]; then
/usr/bin/python3 yourscript.py
else
/usr/bin/python yourscript.py
fi
No, you can't. The shebang line is the first line of the script. There can only be one first line.
While I would suggest always using python3
or a separate wrapper script as detailed in the other answers, it is in fact technically possible to have the Python script serve as its own wrapper script:
#!/bin/sh
"""":
if type python3 > /dev/null 2>&1
then
exec python3 "$0" "$@"
elif type python > /dev/null 2>&1
then
exec python "$0" "$@"
else
echo >&2 "Python not installed"
fi
exit 1
""" #"
print("Hello World")
When run as a Python script, it'll just run as a regular Python script. When run as a shell script, it'll re-invoke itself with python3 or python.
Conforming to POSIX, the execve(2)
system call only has to look at the first line of a script for a #!
-line. So using more than one #!
-line shouldn't work.
According to PEP-394, on UNIX, python2
should exist when a 2.x version of Python is installed, and python3
should exist when a 3.x version of Python is installed.
It is up to the OS distributor to decide if a python
command is present, and if so which version it envokes.
So on UNIX, the is no guarantee that python
will exist.
So I would suggest the following;
#!/usr/bin/env python3
. If this works on all Linux versions you're using, this is the preferred solution.setup.py
. That will ensure a proper shebang-line on installation.(PEP-394 encourages script distributers to foist the use of virtual environments onto users, which IMO is an unnecessary complication for those that do not need to have different versions of a module installed.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With