2

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 or platfrom.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.

hr0m
  • 2,643
  • 5
  • 28
  • 39
  • Please provide full trace back of the error. – Roshin Raphel Jul 20 '20 at 12:57
  • Does this answer your question? [How do I check what version of Python is running my script?](https://stackoverflow.com/questions/1093322/how-do-i-check-what-version-of-python-is-running-my-script) – Tomerikoo Jul 20 '20 at 13:00
  • 2
    The SyntaxError is raised while the module is parsed. At this point no code in the module is executed. This means you can not provide code to handle the SyntaxError in that module. – Klaus D. Jul 20 '20 at 13:05
  • @KlausD. And there is no way in stopping the parser to scan only the first bit of the file, executing that and than continuing? – hr0m Jul 20 '20 at 13:08
  • @hr0m No, since Python isn't interpreted from source, first the whole file is parsed to an abstract syntax tree, then the abstract syntax tree is compiled to bytecode and only then the Python virtual machine runs the bytecode. – Jasmijn Jul 20 '20 at 14:22

1 Answers1

1

I can think of one potential solution for this, where you wrap your entire script in a triple-quoted string (you'll need to make sure that it doesn't conflict with any triple-quoted strings in your script), and pass that to exec, like so:

import sys
if sys.version_info < (3, 7):
    raise ImportError("I need python 3.7 or higher to run!")
exec('''
# your entire script
''')

... at this point I recommend either just using two files, or documenting on your website or wherever what the syntax error means.

Jasmijn
  • 9,370
  • 2
  • 29
  • 43
  • It seems there are no simple solutions. (another possibility i could think of is some magic with shebang and bash. But that is not portable either and may cause problems with environments.) – hr0m Jul 20 '20 at 15:11