Is It Possible To Check If Python Sourcecode Was Written Only For One Version (python 2 Or Python 3)
I make some source code analysis and need to know if the source code was written only for one version (python 2 or python 3). Ideally without starting the script with both runtimes
Solution 1:
You cannot reliably detect that code was written for Python 3 only.
If code uses print
as a statement you can be certain it will only work on Python 2. Similarly, code using except Exception, target:
is only going to run on Python 2.
But it is possible to write polyglot Python code; code that'll run on both Python 2 and 3, by using from __future__
statements; from __future__ import print_function
will let you use the print()
function on Python 2, for example.
Your best bet is to go through the major Python 3 syntax changes, and create a series of heuristics to make an educated guess about the Python version.
The Python-Future project has a helpful cheatsheet that probably is of help here too.
Post a Comment for "Is It Possible To Check If Python Sourcecode Was Written Only For One Version (python 2 Or Python 3)"