Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't import annotations from __future__

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?

like image 388
Shouko Nishimiya Avatar asked Oct 19 '18 09:10

Shouko Nishimiya


People also ask

What is __ future __ in Python?

__future__ module is a built-in module in Python that is used to inherit new features that will be available in the new Python versions.. This module includes all the latest functions which were not present in the previous version in Python. And we can use this by importing the __future__ module.

What is from future import annotations?

It's an enhancement to the existing annotations feature which was initially introduced in python 3.0 and redefined as type hints in python 3.5, that's why your code works under python 3.8. It will become the default in Python 3.10*.

What is from __ future __ import Print_function?

The main function of from __future__ import print_function is to bring the print function from Python 3 into Python 2.


1 Answers

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.

like image 103
Ralf Avatar answered Sep 25 '22 13:09

Ralf