Reputation: 3829
I want to inform the user which python version they should use:
import sys
assert sys.version_info >= (3, 6), "Use Python 3.6 or newer"
print(f"a format string")
But instead, running the above file makes a syntax error:
$ python fstring.py. # default python is 2.7
File "fstring.py", line 3
print(f"a format string")
^
SyntaxError: invalid syntax
Is it possible to do this per-file, without wrapping all the f-strings inside of try blocks?
Upvotes: 5
Views: 3936
Reputation: 51152
No, this is not possible on a per-file basis, because parsing happens for the whole file before any of it is executed, and therefore before any assertions are checked. try
also won't work, for the same reason.
The only way this could possibly work is if you defer the parsing of part of the code until runtime by putting the code in a string and calling eval
, but... don't do that. You have two sensible options: don't use f-strings at all, or let it fail with a SyntaxError
instead of your own custom error message.
Alternatively, if you are working on a Unix or Linux system then you can mark the file as executable and give it a shebang line at the start like #!/usr/bin/python3.8
so that the user doesn't need to know the right version to use themselves.
If you want to do this on a per-module basis instead, see @Chris's answer.
Upvotes: 6
Reputation: 137179
If you are writing a module, you can do this via your module's __init__.py
, e.g. if you have something like
foo_module/
__init__.py
foo_module/
foo.py
setup.py
where __init__.py
contains
import sys
assert sys.version_info >= (3, 6), "Use Python 3.6 or newer"
and foo.py
contains
print(f"a format string")
Example:
Python 2.7.18 (default, Jun 23 2020, 19:04:42)
[GCC 7.5.0] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from foo_module import foo
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "foo_module/__init__.py", line 4, in <module>
assert sys.version_info >= (3, 6), "Use Python 3.6 or newer"
AssertionError: Use Python 3.6 or newer
Upvotes: 5