You can check the version of Python that is running a program, at runtime. Then check the content of the sys. version_info property. This property returns the Python version as a tuple.
Open Command line: Start menu -> Run and type cmd. Type: C:\Python34\python.exe.
Python 3.7. 10, documentation released on 15 February 2021. Python 3.7.
You can test using eval
:
try:
eval("1 if True else 2")
except SyntaxError:
# doesn't have ternary
Also, with
is available in Python 2.5, just add from __future__ import with_statement
.
EDIT: to get control early enough, you could split it into different .py
files and check compatibility in the main file before importing (e.g. in __init__.py
in a package):
# __init__.py
# Check compatibility
try:
eval("1 if True else 2")
except SyntaxError:
raise ImportError("requires ternary support")
# import from another module
from impl import *
Have a wrapper around your program that does the following.
import sys
req_version = (2,5)
cur_version = sys.version_info
if cur_version >= req_version:
import myApp
myApp.run()
else:
print "Your Python interpreter is too old. Please consider upgrading."
You can also consider using sys.version()
, if you plan to encounter people who are using pre-2.0 Python interpreters, but then you have some regular expressions to do.
And there might be more elegant ways to do this.
Try
import platform platform.python_version()
Should give you a string like "2.3.1". If this is not exactly waht you want there is a rich set of data available through the "platform" build-in. What you want should be in there somewhere.
Probably the best way to do do this version comparison is to use the sys.hexversion
. This is important because comparing version tuples will not give you the desired result in all python versions.
import sys
if sys.hexversion < 0x02060000:
print "yep!"
else:
print "oops!"
import sys
# prints whether python is version 3 or not
python_version = sys.version_info.major
if python_version == 3:
print("is python 3")
else:
print("not python 3")
Answer from Nykakin at AskUbuntu:
You can also check Python version from code itself using platform
module from standard library.
There are two functions:
platform.python_version()
(returns string).platform.python_version_tuple()
(returns tuple).Create a file for example:
version.py
)
Easy method to check version:
import platform
print(platform.python_version())
print(platform.python_version_tuple())
You can also use the eval
method:
try:
eval("1 if True else 2")
except SyntaxError:
raise ImportError("requires ternary support")
Run the Python file in a command line:
$ python version.py
2.7.11
('2', '7', '11')
The output of Python with CGI via a WAMP Server on Windows 10:
Sets became part of the core language in Python 2.4, in order to stay backwards compatible. I did this back then, which will work for you as well:
if sys.version_info < (2, 4):
from sets import Set as set
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