Can't import annotations from __future__

Shouko Nishimiya picture Shouko Nishimiya · Oct 19, 2018 · Viewed 17.2k times · Source

When running the statement

from __future__ import annotations

I get the following error:

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

What could be the cause of this error?

Answer

Ralf picture Ralf · Oct 19, 2018

Looking at your error traceback, it looks like you are using python 3.5. Is that the case?

If so, then the error happens because according to PEP-563 the import of __future__ annotations is available starting with Python 3.7.

I did not find any hints that this will be backported to previous versions, but I might have missed that.