Detecting Python major version through source analysis

abingham

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.

starrify

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).

이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.

침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제

에서 수정
0

몇 마디 만하겠습니다

0리뷰
로그인참여 후 검토

관련 기사

분류에서Dev

Logic for verifying major and minor version requirements?

분류에서Dev

version_compare to check for major and minor versions

분류에서Dev

How to catch the exception unsupported major .minor version exception in java

분류에서Dev

anaconda plungin을 사용하는 Pychrm의 pyspark 'Unsupported class file major version 55'

분류에서Dev

java.lang.UnsupportedClassVersionError: module1/MyNewJava: Unsupported major.minor version 51.0

분류에서Dev

Sending mail through python

분류에서Dev

Get에서 Play 오류 : java.lang.UnsupportedClassVersionError : controllers / routes : Unsupported major.minor> version

분류에서Dev

Java 6으로 빌드하려고 할 때 Spring-boot "Unsupported major.minor version 51.0"

분류에서Dev

Iteration Through tuple of dictionaries in Python

분류에서Dev

Detecting all links in markdown files in python and replace them with outputs of string function

분류에서Dev

GL_MAJOR_VERSION에 대한 쿼리에서 "잘못된 열거 자"가 발생합니다.

분류에서Dev

boost_major_version 번호를 자동으로 얻는 방법은 무엇입니까?

분류에서Dev

crawling web page through python regular expression

분류에서Dev

python iterate through binary file without lines

분류에서Dev

Capturing ids by xpath in python from url source

분류에서Dev

Detecting Last View UIViewController

분류에서Dev

Detecting if CancelIoEx canceled synchronously

분류에서Dev

PostgreSQL - Detecting patterns in a series

분류에서Dev

Detecting string overlength in advance

분류에서Dev

Detecting currently active window

분류에서Dev

Detecting IP and Selecting state

분류에서Dev

Detecting application launch event

분류에서Dev

Error in detecting uppercase letters

분류에서Dev

Detecting Texture Collision

분류에서Dev

Issue with pip and virtualenv when specifying python version

분류에서Dev

usbip - Why is linux version different in linux-source package and uname -r?

분류에서Dev

Python "command not found" after trying new to move to new python version

분류에서Dev

How to change python version used by python-green

분류에서Dev

xaml parser is not detecting my converter

Related 관련 기사

  1. 1

    Logic for verifying major and minor version requirements?

  2. 2

    version_compare to check for major and minor versions

  3. 3

    How to catch the exception unsupported major .minor version exception in java

  4. 4

    anaconda plungin을 사용하는 Pychrm의 pyspark 'Unsupported class file major version 55'

  5. 5

    java.lang.UnsupportedClassVersionError: module1/MyNewJava: Unsupported major.minor version 51.0

  6. 6

    Sending mail through python

  7. 7

    Get에서 Play 오류 : java.lang.UnsupportedClassVersionError : controllers / routes : Unsupported major.minor> version

  8. 8

    Java 6으로 빌드하려고 할 때 Spring-boot "Unsupported major.minor version 51.0"

  9. 9

    Iteration Through tuple of dictionaries in Python

  10. 10

    Detecting all links in markdown files in python and replace them with outputs of string function

  11. 11

    GL_MAJOR_VERSION에 대한 쿼리에서 "잘못된 열거 자"가 발생합니다.

  12. 12

    boost_major_version 번호를 자동으로 얻는 방법은 무엇입니까?

  13. 13

    crawling web page through python regular expression

  14. 14

    python iterate through binary file without lines

  15. 15

    Capturing ids by xpath in python from url source

  16. 16

    Detecting Last View UIViewController

  17. 17

    Detecting if CancelIoEx canceled synchronously

  18. 18

    PostgreSQL - Detecting patterns in a series

  19. 19

    Detecting string overlength in advance

  20. 20

    Detecting currently active window

  21. 21

    Detecting IP and Selecting state

  22. 22

    Detecting application launch event

  23. 23

    Error in detecting uppercase letters

  24. 24

    Detecting Texture Collision

  25. 25

    Issue with pip and virtualenv when specifying python version

  26. 26

    usbip - Why is linux version different in linux-source package and uname -r?

  27. 27

    Python "command not found" after trying new to move to new python version

  28. 28

    How to change python version used by python-green

  29. 29

    xaml parser is not detecting my converter

뜨겁다태그

보관