0

Is there a tool for detecting the major Python version being used in a code base? I need to be able to examine source code and determine which version of Python to use to execute that code.

One approach I've considered is seeing if 2to3 suggests any changes. Any suggestions would indicate Python 2, otherwise I could assume Python 3. This feels slightly kludgy, though, so I wonder if there isn't a better approach.

3
  • 1
    This isn't actually possible in general. You can do things like detect syntax features that only exist in one major version (e.g. print statement indicates Python 2), but if a program is syntactically valid in both Python 2 and 3, all you can really do is guess. Commented Sep 21, 2014 at 8:55
  • That's a good point. I guess a better statement of the question is how to detect if the code is necessarily one or the other. If it could be either, then I'll have to make a human choose. Commented Sep 21, 2014 at 9:00
  • You really need to bake in a ton of heuristics if you want this to work well. But what that large amount of code that works on both versions? Commented Sep 21, 2014 at 9:18

2 Answers 2

2

A rough idea:
Compile the source with py_compile, do it twice with Python2 and Python3 respectively and see if an error is given.

This is to distinguish from syntax. A piece of source which fits both 2 and 3 could be simply judged to be "both".

Also this is a static method and cannot infer from information that requires runtime, e.g.:

import a_python_2_specific_module

implies that the code is Python2-only, but detecting this fact seems beyond by method's ability.

ATTENTION:

Please notice that it is strongly not recommended if you actually invoke untrusted source code (like feed it to an interpreter, or eval).

Sign up to request clarification or add additional context in comments.

4 Comments

Note that even non-malicious code can have nontrivial dangerous side-effects.
This is the flavor of approach I think I'm going to use. One potential problem with this idea is that compilation will fail for a number of errors that have nothing to do with 2/3 differences (e.g. syntax errors) so it may turn out that using 2to3 is more robust, but only experimentation will really tell.
@abingham: 2to3 does no version detection; why would it? It assumes its input is written for Python 2. It's not going to look at Python 3 source code and say "job's done"; it'll make changes like print(1, 2) -> print((1, 2)).
@user2357112: Good point. I didn't realize 2to3 behaved this way. FWIW, I didn't expect 2to3 to do "version detection" per se, but I was hoping I could be lazy and rely on it to "do nothing" with Python 3 code. The more feedback I get on this, the more it's clear that I'll really just need to implement a collection of individual tests a la 2to3.
0

you can use eval inside your code for check :

try:
  eval("1 if True else 2")
except SyntaxError:
  #

or some future that not in all of versions !

1 Comment

Well, depends on where the OP gets the source code. For untrusted source codes I strongly discourage to run them.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.