开发者

I want my Python script to detect the version and quit gracefully in case of a mismatch

开发者 https://www.devze.com 2022-12-21 14:59 出处:网络
I\'d like to make it as general as possible - e.g. handle as many versions as possible. Since version 3 is not backwards compatible with version 2, I want to make sure that I use the right print stat

I'd like to make it as general as possible - e.g. handle as many versions as possible.

Since version 3 is not backwards compatible with version 2, I want to make sure that I use the right print statement.

Please let me know if you have questions and feel free to share related knowledge having to do with dynamic logic based on what (e.g. libraries) is available.

Suppose I have a script that will run under version 1.x, or 2.x, or 3.x only.

Or a script which requires a library A or a library B.

Thanks!

EDIT:

Now ... when it comes to Python (unlike .Net) some libraries like SciPy, Google App Engine keep you glued to particular version. On Linux, Mac Os you can switch between different Python installs on command line. This is why I want to avoid confusion - I want开发者_Go百科 to remember which script is for which version of Python and what libraries it needs. I would rather have it fail in a human-readable way.


The sys module also contains the version info (first available in version 2.0):

import sys

if sys.version_info[0] == 2:
    print("You are using Python 2.x")
elif sys.version_info[0] == 3:
    print("You are using Python 3.x")


In order to get around syntax errors you would have to use conditional imports, if you want to mix syntax between versions 2 and 3.

# just psuedocode
if version is x:
   import lib_x # contains version x implementation
else:
   import lib_y # contains version y compatible implementation

It is not advisable to try to maintain compatibility between python3 and older versions. That said, here are several ways of detecting the version of python being used:

While not particularly human friendly, sys.hexversion will work across the widest variety of python versions, since it was added back in version 1.5.2:

import sys
if sys.hexversion == 0x20505f0:
    print "It's version 2.5.5!"

You should be able to get version information from sys.version_info (added in python 2.0):

import sys
if sys.version_info[0] == 2:
    print "You are using version 2!"
else:
    print "You are using version 1, because you would get SyntaxErrors if you were using 3!"

Alternatively you could use the platform module, though that was introduced in python 2.3, and would not be available in older versions (assuming you even care about them):

try:
    import platform
    if platform.python_version().startswith('2'):
        print "You're using python 2.x!"
except ImportError, e:
    print "You're using something older than 2.3. Yuck."


  • If you want to maintain a codebase that works with Python 2 and 3, you wouldn't try to make code that will run in both, which will be awkward and ugly and bugprone, you would write in Python 2 and use 2to3 to convert. (You can also write in Python 3 and use 3to2 to convert, but I believe that tool is less mature.) 2to3 is not perfect, but making Python 2 code that can be converted by it makes tons more sense than making Python 2 code that will run in a Python 3 interpretter. Edit: This was common wisdom in 2010, but turned out not to be the case. The thing to do is write a polyglot. 2to3 did not end up being all that useful

  • Another option is Cython, a Python-like language that can be used to create C extension modules. Cython modules can be used with Python 2 and 3.

  • When you support multiple versions of Python, it is generally better to directly check for the capability you want rather than to check a version number. Checking version numbers directly is fragile and indirect.

    For example, if I wanted code that would work with Python pre-2.5, I would say:

    try: 
        any
    except NameError:
        def any(iterable):
            for item in iterable:
                if item:
                    return True
            return False
    

    (note that this is prettymuch the only reason to catch NameError). Similarly, library availability would be checked by catching ImportError. Edit: There is a tons of tooling for abstracting this away by now.

  • If you want a script to remember what version it is from, like you say, don't bother trying to support multiple versions at all. Put the proper version number binary in the shebang line and run the script based on that.


If the only issue is that you want to use the right print statement to avoid syntax errors, you can avoid the problem altogether by using the print() function in Python 2.6:

if sys.version_info[0:2] == (2,6):           # Or you could use try/except here
    from __future__ import print_function
print("Now you can use print as a function regardless of whether you're on 2.6 or 3.x!")

Of course, if you also want to support earlier versions of Python, this won't work.


FYI, if you ever want to port 2.x scripts to 3.x, you can use 2to3 source conversion tool.


On Linux, Mac etc, you should use the standard first line:

#!/usr/bin/env python2

or

#!/usr/bin/env python2.6

or

#!/usr/bin/env python3

On Windows, having such a first line is useful from a documentation point of view, even if Windows doesn't use it to choose the interpreter.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号