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.
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.
Please notice that it is strongly not recommended if you actually invoke untrusted source code (like feed it to an interpreter, or eval
).
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다