3 ms·
While this looks funny and smart, it's not really a good solution as it lacks well communicated documentation. Python has a proper way to check for version, whi
by slightwinder 5y ago
While this looks funny and smart, it's not really a good solution as it lacks well communicated documentation. Python has a proper way to check for version, which is backward-compatible till version 2.0, so around 20+ years.
import sys
if sys.version_info.major < 3:
raise Exception('Need Python 3 or higher')
EDIT: correction, sys.version_info.major is only there since Python 2.7. For older versions sys.version_info[0] should be used.
- tyingq 5y ago>so around 20+ years. Right, but if there's anything below that that would cause a syntax error, like an f-string, that Exception you created never happens. The end user just sees a syntax error. It's unfortunate there's not a better way to handle it, especially with so many people still having /usr/bin/python linked to a 2.x version.
- deleted 5y ago[deleted]
- maple3142 5y agoI think this is worse because once you have a python 3 only syntax in your script (such as f-string), python 2 will simply throw syntax error because it need to parse the file to execute. The original solution looks hacky, but it can actually inform user to use python 3.