2

Is there an implemented program solution to identify which version of the Python interpreter is supported for an undocumented .py module except for manually looking into the source code?

$ python get_py_version my_module.py
'2.6 version module'

Problem arose after installing an undocumented package in 3.x interpreter to discover during the import there are print statements from 2.x interpreter

Chesnokov Yuriy
  • 1,760
  • 5
  • 21
  • 35
  • Nope, looking at the code is pretty much it. Unless the developer put in documentation (docstrings, comments, or an explicit version check). – Martijn Pieters Jun 02 '15 at 08:00
  • Simply run module test suite - if all tests pass, module is supported by your interpreter. If this is bare module without any tests - your guess is as good as mine. As far as I know there is no generic solution for this task. Of course you may try to use 2to3 and prepare your module manually, but problem still occurs - without test suite you cannot know if code 'works'. Any code. – Łukasz Rogalski Jun 02 '15 at 08:00
  • @MartijnPieters that is a missing feature in Python, it would be easy to implement as a part of interpreter to infer the supported version from module syntax – Chesnokov Yuriy Jun 02 '15 at 08:13
  • @ŁukaszR. there is no test suite, what if I installed 3.x version in 2.x interpreter. I was hoping the already implemented program solution was available to infer the version from module source code – Chesnokov Yuriy Jun 02 '15 at 08:15
  • Ask the person who wrote the module. – Peter Wood Jun 02 '15 at 08:18
  • @ChesnokovYuriy I think it would be pretty hard to implement non-ambiguous algorithm. `print()` is trivial, but a lot of other stuff is not. Also, some modules may be 100% suitable to get "Python 2.0" or even maybe "1.x" as interpreter version. – Łukasz Rogalski Jun 02 '15 at 08:19
  • @ChesnokovYuriy: that's nigh impossible; that'd require an endless list of modules to track as well as syntax. Python has a long history of excellent forward compatibility as well, with the only major exception being the Python 2 to 3 transition. – Martijn Pieters Jun 02 '15 at 08:32
  • @ŁukaszR. having a module to estimate the lowest supported version or something – Chesnokov Yuriy Jun 02 '15 at 09:37
  • @MartijnPieters then it might get started as the 2.x or the 3.x version check – Chesnokov Yuriy Jun 02 '15 at 09:39

1 Answers1

1

Because this is just a text file, there is no way for you to automagically find out what version of Python it was written in. And I don't know if there is much motivation to add this in to the language, especially considering how glaringly obvious it is when you have a py3 vs a py2 file (search for print, check future imports etc.).

However you can tell the difference if you are looking at a .pyc file. As these were compiled by a specific interpreter, there is normally a component of the file that gives the hex value for the interpreter used.

See here for more detail: Is there a way to know by which Python version the .pyc file was compiled?

Community
  • 1
  • 1
NDevox
  • 4,056
  • 4
  • 21
  • 36