Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

version

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 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.

like image 907
Hamish Grubijan Avatar asked Nov 28 '22 19:11

Hamish Grubijan


1 Answers

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")
like image 96
cschol Avatar answered Dec 05 '22 05:12

cschol