i have thousands of servers(linux), some only has python 2.x and some only has python 3.x, i want to write one script check.py can run on all servers just as $./check.py without use $python check.py or $python3 check.py, is there any way to do this?
my question is how the script check.py find the Interpreter no matter the Interpreter is python2.x and python3.x
Many scripts can run on both 2.x and 3.x. (I've got a bunch I work on on a daily basis, and I've converted various open source libraries from 2.x-only to dual-version.)
A few things make it much easier:
u
prefix being taken away) were reversed in 3.2 or 3.3.__future__
statements.bytes
(always 8-bit), unicode
(must encode if you want 8-bit), or str
(whatever most of the stdlib APIs expect), and encode
and decode
as necessary.2to3
on your code. (But don't blindly do everything it says. If, e.g., you're using d.keys()
or map(f, l)
because you don't care whether you get back a list
or not, you'll get a warning, because 2to3
doesn't know you don't care.)Alternatively, instead of trying to write code that runs on both, write code that runs on 2.x, but can be automatically transformed by 2to3
into running 3.x code, and make that part of your installation process (in setup.py
, if sys.version_info >= (3, 0):
do the 2to3
step).
From your edit, it sounds like you're mostly concerned with what to put in the #! line. For that:
/usr/bin/env python
This isn't guaranteed to work—but then env
isn't guaranteed to work in the first-place… You can count on the fact that:
python
is Python 2.python
is Python 2.python
is Python 3.However:
python
. There's not much you can do about this one.If the last one is a serious problem, you can work around it by adding a launcher script, written in sh, that tries python
and then tries python3
if that fails.
The nice way to do this is to specify the launcher script itself as the shebang interpreter in your Python script. Linux can handle this, but it's configurable, and at least some distros disable it by default—and most other *nix systems can't do it.
If that doesn't work, the next best option is to make the user run the launcher script—that is, tell them to do ./check.sh
instead of ./check.py
, and check.sh
figures out the right Python interpreter and runs $python ./check.py
for the user.
If you want to get really tricky, you could even embed the Python script as a heredoc inside the shell script, so you only need to distribute one file. They run ./check.sh
, and it finds the right Python and runs it on the heredoc.
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