Can't import annotations from __future__
Asked Answered
F

2

69

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?

Fireplace answered 19/10, 2018 at 9:39 Comment(0)
M
69

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.

Marry answered 19/10, 2018 at 10:1 Comment(8)
Thanks. The problem fixed after installing Python 3.7.Fireplace
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?Kegler
Mabe because those annotations are planned for Python 4.0.Aquatint
the future import is not about annotations, it's about postponed evaluation of annotations.Upstretched
For anyone coming along now, this appears to have been back-ported to 3.6.9Frenchman
I don't think it is. Running Python 3.6.9 and I am still getting SyntaxError: future feature annotations is not definedIntrude
Is Python 3.7 tested with Ubuntu 16? It doesn't seem to be an apt package.Gio
@Gio 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.Marry
M
1

If it happens with Python 3.6, do:

pip3 install --upgrade pip

or

pip install --upgrade pip
Misfeasance answered 20/7, 2023 at 18:31 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.