I want my Python script to detect the version and exit gracefully in case of inconsistency

I would like to make this as general as possible - for example, process as many versions as possible.

Since version 3 does not support backward compatibility with version 2, I want to make sure that I am using the correct print instruction. A.

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

Suppose I have a script that will only work in versions 1.x or 2.x or 3.x.

Or a script that requires library A or library B.

Thanks!

EDIT:

Now ... when it comes to Python (as opposed to .Net), some libraries, such as SciPy, the Google App Engine, stick to a specific version. On Linux, Mac Os, you can switch between different Python installations on the command line. That's why I want to avoid confusion - I want to remember which script is for which version of Python and for which libraries it is needed. I would prefer that this does not get credibility.

+4
source share
6 answers
  • If you want to maintain a code base that works with Python 2 and 3, you would not try to make code that will work in both, which would be inconvenient and ugly and an error, you should write in Python 2 and use 2to3 to convert. (You can also write in Python 3 and use 3to2 for conversion, but I think this tool is less mature.) 2to3 not perfect, but Python 2 code that can be converted to it makes more sense than creating code Python 2, which will be launched in the Python 3 interpreter. Edit: this was common wisdom in 2010, but it turned out that it wasn’t. The fact is that you need to write a polyglot. 2to3 was ultimately not useful

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

  • When you support multiple versions of Python, it is usually best to directly check the feature you want, rather than checking the version number. Checking version numbers directly is fragile and indirect.

    For example, if I needed code that worked 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 a pretty simple reason to catch a NameError ). Similarly, library availability will be checked by catching an ImportError . Edit: At the moment, there are many tools to abstract this.

  • If you want the script to remember which version it is from, as you say, do not try to support multiple versions at all. Put the correct version number in the binary in the shebang line and run the script based on this.

+3
source

The sys module also contains version information (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") 
+8
source

To get around syntax errors, you will 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 recommended that you try to maintain compatibility between python3 and older versions. However, here are a few ways to discover which version of python is being used:

While sys.hexversion not particularly human-friendly, it will work in a wide variety of versions of python since it was added back to version 1.5.2:

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

You can 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 can use the platform module, although this was introduced in python 2.3 and will not be available in older versions (if 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." 
+7
source

If the only problem is that you want to use the correct print statement to avoid syntax errors, you can generally avoid the problem 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 will not work.

+3
source

FYI, if you ever need a 2.x script port on 3.x, you can use the 2to3 source conversion tool .

+2
source

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, this first line is useful from a documentation point of view, even if Windows does not use it to select an interpreter.

+2
source

Source: https://habr.com/ru/post/1302948/


All Articles