Is it possible to check if python source code was written for only one version (python 2 or python 3)

I am doing some analysis of the source code and should know if the source code was written for only one version (python 2 or python 3). Ideally, without running a script with both time modes and checking if this throws an exception. I do not want to check which of them are installed as here .

Maybe I'm looking for some kind of linter that validates typical python 2 code, like

type hello

instead

print (hello)

It is my problem. I read a file like this:

for i in range(10):
    print(True) if (i % 2 == 1) else print(False)

which works fine under python 3 and causes an error for python2

print(True) if (i % 2 == 1) else print(False)
                                         ^
SyntaxError: invalid syntax

Maybe with compiler , but it is marked as deprecated

linter, , python 2 3? ?

+4
1

, Python 3.

print , , Python 2. , except Exception, target: Python 2.

Python; , Python 2, 3, from __future__ statement; from __future__ import print_function print() Python 2. :

Python 3 , Python .

Python-Future cheatsheet, , , .

+1

All Articles