Using features from newer python versions, e.g. f-string debugging feature: f'{some_var=}'
, results into a SyntaxError
.
Suppose I have a python script which I would like to provide, and the user executes said script with an old python version, he will just get this error. Instead I would like to provide him with some meaningful text, e.g. "Please update python to version >=3.7"
I can solve it with a main
file, which checks the version and then imports my script.
Is there a way to achieve this, while still having only a single file script?
Possible approaches:
- Check
sys.version
orplatfrom.python_version_tuple
-> Not possible,SyntaxError
gets in the way, as python parses whole files - Use eval to determine
SyntaxError
: -> Not possible for same reasons
try:
a = "hello"
eval("f'{a=}'")
except SyntaxError:
raise ImportError('Update your Python version!!!!')
Can I trick Python somehow to not syntactically check the whole file?
(I could "pack" the whole file into a string, check for the version and then eval
the string, but that is not a very clean solution and it is terrible for development)
Edit:
This question is not about "HOW to check for python version". Instead it is about "How to check for python version, before I receive SyntaxError
due to new features.