64

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?

Ralf
  • 16,086
  • 4
  • 44
  • 68
Shouko Nishimiya
  • 743
  • 1
  • 5
  • 4

2 Answers2

67

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.

Ralf
  • 16,086
  • 4
  • 44
  • 68
  • Thanks. The problem fixed after installing Python 3.7. – Shouko Nishimiya Oct 19 '18 at 17:13
  • 22
    This future feature is also missing in Python 3.6. Why isn't it back ported? If I use annotations, they are widely supported in 3.7, so no need for a future. If I run my code on an older Python, both, the annotations and the future are not supported. So why this future? – Paebbels Apr 21 '19 at 21:41
  • 3
    Mabe because those annotations are planned for Python 4.0. – Matthias Dec 17 '19 at 07:51
  • 5
    the future import is not about annotations, it's about postponed evaluation of annotations. – Quickbeam2k1 May 15 '20 at 06:52
  • 4
    For anyone coming along now, this appears to have been back-ported to 3.6.9 – SiHa Sep 13 '22 at 15:47
  • 3
    I don't think it is. Running Python 3.6.9 and I am still getting `SyntaxError: future feature annotations is not defined` – wheeler Mar 16 '23 at 16:51
  • Is Python 3.7 tested with Ubuntu 16? It doesn't seem to be an `apt` package. – personal_cloud Aug 30 '23 at 04:58
  • @personal_cloud I am not sure that I understand your question/comment; the `__future__ annotations` is a Python package and not an Ubuntu/`apt` package. Also, this `__future__ annotations` package is included in the standard Python installation (depending on the version) and as far as I know it is not available as a separate package for optional installation. – Ralf Aug 31 '23 at 11:06
0

If it happens with Python 3.6, do:

pip3 install --upgrade pip

or

pip install --upgrade pip
anask
  • 335
  • 3
  • 5