无法从__future__导入注释

Sho*_*iya 9 python import future python-3.x

运行语句时

from __future__ import annotations
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

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
Run Code Online (Sandbox Code Playgroud)

造成此错误的原因是什么?

Ral*_*alf 8

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

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

我没有发现任何暗示将其反向移植到以前的版本,但是我可能会错过这一点。

  • Python 3.6 中也缺少此未来功能。为什么不回传?如果我使用注释,它们在 3.7 中得到广泛支持,所以不需要未来。如果我在较旧的 Python 上运行我的代码,则不支持注释和未来。那么为什么是这个未来呢? (9认同)
  • 未来的导入与注释无关,而是与注释的延迟评估有关。 (6认同)
  • 我不认为是这样。运行 Python 3.6.9,我仍然收到“SyntaxError:未定义未来功能注释” (5认同)
  • 对于现在的任何人来说,这似乎已向后移植到 3.6.9 (4认同)
  • 也许是因为这些注释是为 Python 4.0 计划的。 (2认同)