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.

5
  • Please provide full trace back of the error. Commented Jul 20, 2020 at 12:57
  • Does this answer your question? How do I check what version of Python is running my script? Commented Jul 20, 2020 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. Commented Jul 20, 2020 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? Commented Jul 20, 2020 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. Commented Jul 20, 2020 at 14:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.