Is there a standard way to make sure a python script will be interpreted by python2 and not python3?

白昼怎懂夜的黑 提交于 2019-11-30 08:22:09

This is a bit of a messy issue during what will be a very long transition time period. Unfortunately, there is no fool-proof, cross-platform way to guarantee which Python version is being invoked, other than to have the Python script itself check once started. Many, if not most, distributions that ship Python 3 are ensuring the generic python command is aliased by default to the most recent Python 2 version while python3 is aliased to the most recent Python 3. Those distributions that don't should be encouraged to do so. But there is no guarantee that a user won't override that. I think the best practice available for the foreseeable future is to for packagers, distributors, and users to assume python refers to Python 2 and, where necessary, build a run-time check into the script.

http://docs.python.org/library/sys.html#sys.version_info

using the sys module you can determine the version of python that is running and raise an exception or exit or whatever you like.

UPDATE:

You could use this to call the appropriate interpreter. For example, set up a small script that does the checking for you, and use it in the shbang. It would check the python version running, and if not what you want, looks for one you want. Then it would run the script in that version of python (or fail if nothing good was found).

Using sys.version_info you can do a simple value test against it. For example if you only want to support version 2.6 or lower:

import sys
if sys.version_info > (2,6):
    sys.exit("Sorry, only we only support up to Python 2.6!")

Not quite the same situation, but the company I work for has an app that can run Python scripts (among its many features). After numerous support issues involving Python installations on various platforms, we decided to just install our own Python interpreter with the app. That way we know exactly where it is installed and what version it is. This approach may be too heavyweight for your needs (the Python package is only about 10% of our app's bits) but it definitely works.

Depends on how you're distributing it, I guess.

If you're using a normal setup.py file to manage your distribution, have it bomb out if the user is trying to install it in Python 3.

Once it's installed, the shebang of the console script created by (say) setuptools will likely be linked to the specific interpreter used to install it.

If you're doing something weird for your installation, you can in whatever installation script you're using look for python interpreters and store a choice. You might first check whether whatever is called "python" is a 2.x. If not, check for "python2.7", "python2.6", etc to see what's available.

As I understand different distros will be in different locations in your drive. Here are some suggestions that come to mind -

  1. You could use UNIX alias to create shortcuts pointing to the different distros. Eg: alias py2="/usr/bin/python2.X". So when you run your script you could use py2 xx.py
  2. Or other way could be to modify your PYTHON_PATH environment variable.
  3. Or if I am not wrong there is a provision in sys module to get the current python version number. You could get that & deal appropriately.

This should do it...

Alex Nelson

You can use the autotools to pick a Python 2 interpreter. Here is how to do that. Guaranteeing a correct shebang may be tricky to do elegantly; here is one way to do that. It may be easier to simply have a light Bash wrapper script, wrapper.sh.in that looks something like:

#!/bin/bash
PYTHON2="@PYTHON@" #That first link enables this autotool variable
"$PYTHON2" "$@"  #Call the desired Python 2 script with its arguments

Call wrapper.sh (after a ./configure) like:

./wrapper.sh my_python2_script.py --an_option an_argument

I believe this will do what you want, namely test for a non-specific version of Python less than 3.x (as long as it doesn't contain a from __future__ import print_function statement).

try:
    py3 = eval('print')
except SyntaxError:
    py3 = False

if py3: exit('requires Python 2')
...

It works by testing to see if print is a built-in function as opposed to a statement, as it is in Python3. When it's not a function, the eval() function will raise an exception, meaning the code is running on a pre-Python 3.0 interpreter with the caveat mentioned above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!