"from __future__ import should occur at the beginning of the file": what determines the beginning of the file?

Python script

'''
a
'''

from __future__ import print_function

works well (i.e. does nothing), but

'''
a
'''

'''
b
'''
from __future__ import print_function

causes:

File "C:\test.py", line 8
    from __future__ import print_function
SyntaxError: from __future__ imports must occur at the beginning of the file

Why?


https://docs.python.org/2/reference/simple_stmts.html#future says that:

The following statement should appear at the top of the module. The only lines that may appear before a future expression:

  • docstring module (if any),
  • comments
  • empty lines and
  • other future statements.

The second example contains only comments and empty lines before from __future__ import print_function, but still it does not work.

I am using Python 2.7.

+5
source share
1 answer

... , , , .

, , .

docstring, , . __future__ , , , .

+11

Source: https://habr.com/ru/post/1682867/


All Articles