Check Python version from inside script? Run Pythons script in v2 compatibility mode?

Terry Reedy tjreedy at udel.edu
Fri Jul 7 02:59:39 EDT 2017


On 7/7/2017 2:30 AM, Ben S. via Python-list wrote:
> Can I somehow check from inside a Python script if the executing Python engine is major version v2 or v3?
>> I am thinking about a code similar to
>> if (os.python-majorversion<3)
> print hello
> else
> print (hello)

For this, just use print('hello').
Checkout 'from __future__ import print_function'
check __future__ module for spelling.
Checkout sys.version, sys.hexversion, platform module,
Some import will tell you.
try:
 import tkinter as tk
 pyversion = 3
except ImportError:
 import Tkinter as tk
 pyversion = 2
>> Additional question:
> Is there a way to execute a python script with v3 python engine in v2 compatibility mode?
> I am thinking about a command parameter like (python.exe is v3.*):
>> python.exe -execute_as_v2 myscript.py

No.
-- 
Terry Jan Reedy


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /