Tool to determine what lowest version of Python required?
Inspired by this excellent question, I recently put together a script that tries to do this. You can find it on github at pyqver.
It's reasonably complete but there are some aspects that are not yet handled (as mentioned in the README file). Feel free to fork and improve it!
Not an actual useful answer but here it goes anyway. I think this should be doable to make (though probably quite an exercise), for example you could make sure you have all the official grammars for the versions you want to check, like this one .
Then parse the bit of code starting with the first grammar version. Next you need a similar map of all the built-in module namespaces and parse the code again starting with the earliest version, though it might be tricky to differentiate between built-in modules and modules that are external or something in between like ElementTree.
The result should be an overview of versions that support the syntax of the code and an overview of the modules and which version (if at all) is needed to use it. With that result you could calculate the best lowest and highest version.
The tool pyqver from Greg Hewgill wasn't updated since a while.
vermin is a similar utility which shows in the verbose mode (-vvv
) what lines are considered in the decision.
% pip install vermin
% vermin -vvv somescript.py
Detecting python files..
Analyzing using 8 processes..
!2, 3.6 /path/to/somescript.py
L13: f-strings require 3.6+
L14: f-strings require 3.6+
L15: f-strings require 3.6+
L16: f-strings require 3.6+
print(expr) requires 2+ or 3+
Minimum required versions: 3.6
Incompatible versions: 2
Bonus: With the parameter -t=V
you can define a target version V
you want to be compatible with. If this version requirement is not met, the script will exit with an exit code 1
, making it easy integratable into a test suite.