из-за того, что когда вы выполняете import math
, вы импортируете все классы функций или что-либо еще в математической библиотеке, чем используете больше оперативной памяти и т. Д., Но при выполнении from math import sqrt
вы импортируете только функцию sqrt во все математические библиотеки
Вдохновленный этим замечательным вопросом, я недавно написал сценарий, который пытается это сделать. Вы можете найти его на github в pyqver .
Он достаточно завершен, но есть некоторые аспекты, которые еще не обработаны (как упомянуто в файле README). Не стесняйтесь раскошелиться и улучшить его!
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.