我尝试在文件的前几行中使用版本检查来执行此操作:
import sys
if not sys.version_info[0] == 3:
sys.exit('You need to run this with Python 3')
# Lots of other code here....
# And then...
foo = 2
bar = 3
print(f"Some message that uses f-strings like this: variables are {foo} {bar}")
但是,当.py文件实际使用Python2运行时,它在使用f字符串的行上返回语法错误。这不太容易理解。
有没有办法可以强制程序在版本检查时失败,从而返回更有用的错误消息?
谢谢