Running AquaEmacs, I want to execute a buffer (C-c C-c) in Python.
The buffer starts with:
from __future__ import print_function
The execution in AquaEmacs starts with:
import sys,imp
if'test_one_liners' in sys.modules:
imp.reload(test_one_liners)
else:
import test_one_liners
where test_one_liners.py is my file. This gives this error:
SyntaxError: from __future__ imports must occur at the beginning of the file
Anyone know where and how to fix this?
Let me add the information again to make it clear.
Create this buffer:
from __future__ import print_function
print("Hello")
Use File/Change Buffer Mode/Python to make it Python and C-c C-c to execute it; it need not be saved. The buffer is written to some temporary file and fails with SyntaxError: from __future__ imports must occur at the beginning of the file
. The traceback includes a generated file which contains a reference to a temp file. What should happen instead it printing Hello.
Even PEP 328 that defines the absolute_import line behavior is not entirely clear. I eventually resorted to spelunking in the Python 2.5 and 2.7 source code to see how it worked. tl;dr: Importing absolute_import from the __future__ module changes the behavior of implicit relative imports within a Python 2 program.
To avoid confusing existing tools that analyze import statements and expect to find the modules they’re importing. To ensure that future statements run under releases prior to 2.1 at least yield runtime exceptions (the import of __future__ will fail, because there was no module of that name prior to 2.1).
tl;dr: Importing absolute_import from the __future__ module changes the behavior of implicit relative imports within a Python 2 program. Since Python 3 disallows that type of import, they must be eliminated from code that is to run under both versions of Python.
This was a bug in python-mode.el
which is fixed in the current release. It looks like aquamacs is bundled with an older version of python-mode.el
.
The bug report is here: bug #1063884 and the fix here.
You should update your python-mode.el package.
You say from __future__ import print_function
, but the error message talks about from __future__ imports
--- Note the s
at the end of that.
You say that " test_one_liners.py is my file". And you say that from __future__ import print_function
is at the beginning of "the buffer". But the error messages says that it doesn't find that incantation at the beginning of file test_one_liners.py
. Perhaps your current buffer, which has that incantation, is not for that file?
But if I had to guess, I'd guess the problem is the apparent typo in #1. ("Apparent", because you might just have mistyped the post, and the incantation might be correct in the file.)
As it was said, this is fixed in current trunk.
https://launchpad.net/python-mode
When checking your example, get "ImportError: No module named test_one_liners", which is to expect.
Second example prints nicely "Hello".
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With