【问题标题】:Can't import annotations from __future__无法从 __future__ 导入注释
【发布时间】:2019-03-24 04:35:12
【问题描述】:

运行语句时

from __future__ import annotations

我收到以下错误:

Traceback (most recent call last):
  File "/usr/lib/python3.5/py_compile.py", line 125, in compile
    _optimize=optimize)
  File "<frozen importlib._bootstrap_external>", line 735, in source_to_code
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "./prog.py", line 1
    from __future__ import annotations
    ^
SyntaxError: future feature annotations is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/lib/python3.5/py_compile.py", line 129, in compile
    raise py_exc
py_compile.PyCompileError:   File "./prog.py", line 1
    from __future__ import annotations
                                     ^
SyntaxError: future feature annotations is not defined

此错误的原因可能是什么?

【问题讨论】:

    标签: python python-3.x import future


    【解决方案1】:

    查看您的错误回溯,您似乎正在使用python 3.5。是这样吗?

    如果是这样,则发生错误,因为根据PEP-563__future__ annotations 的导入可以从Python 3.7 开始。

    我没有发现任何暗示这将被向后移植到以前的版本,但我可能错过了。

    【讨论】:

    • 谢谢。安装 Python 3.7 后问题已修复。
    • Python 3.6 中也缺少此未来功能。为什么不向后移植?如果我使用注释,它们在 3.7 中得到广泛支持,因此不需要未来。如果我在较旧的 Python 上运行我的代码,则不支持注释和未来。那么为什么会有这个未来呢?
    • Mabe 因为这些注解是为 Python 4.0 计划的。
    • 未来的导入不是关于注解,而是关于注解的延迟评估。
    猜你喜欢
    • 1970-01-01
    • 2019-07-26
    • 2021-09-09
    • 2021-03-13
    • 1970-01-01
    • 1970-01-01
    • 2021-04-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多