Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute AquaMacs buffer that has "from __future__ import ..."

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.

like image 886
Charles Merriam Avatar asked Sep 07 '13 16:09

Charles Merriam


People also ask

Does absolute_import change the behavior of implicit relative imports?

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.

Why do we need to know the module name before importing?

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).

What does absolute import do in Python 3?

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.


3 Answers

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.

like image 146
Carl Groner Avatar answered Oct 20 '22 11:10

Carl Groner


  1. You say from __future__ import print_function, but the error message talks about from __future__ imports --- Note the s at the end of that.

  2. 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.)

like image 35
Drew Avatar answered Oct 20 '22 10:10

Drew


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".

like image 1
Andreas Röhler Avatar answered Oct 20 '22 09:10

Andreas Röhler