如何优雅地处理未来的功能导入失败?如果用户正在使用Python 2.5运行,并且我的模块中的第一个语句是:
from __future__ import print_function
复制代码
为Python 2.5编译此模块将失败,并显示:
File "__init__.py", line 1
from __future__ import print_function
SyntaxError: future feature print_function is not defined
复制代码
我想通知用户,他们需要使用Python> = 2.6重新运行该程序,并且可能会提供一些有关如何执行此操作的说明。但是,引用PEP 236:
The only lines that can appear before
a future_statement are:
The module docstring (if any).
Comments.
Blank lines.
Other future_statements.
复制代码
所以我不能做像: