5

Attempting to eval a babel source block that contains a __future__ import and also uses a :var causes Python to spit out

  File "<stdin>", line 2
SyntaxError: from __future__ imports must occur at the beginning of the file

Presumably babel is sticking the :var definitions before the first line of my code. How can I prevent babel from doing this?

Minimum failing example:

#+NAME: data
| Hi | 2 |

#+BEGIN_SRC python :var data=data
  from __future__ import print_function
  print(*data)
#+END_SRC
4

You can use the prologue header argument. Like this:

#+NAME: data
| Hi  | 2 |
| Bye | 3 |

#+name: test-py27-with-print-function
#+header: :prologue "from __future__ import print_function"
#+BEGIN_SRC python :var data=data :results output list
  for row in data:
      print(*row)
#+END_SRC

#+RESULTS: test-py27-with-print-function
: - Hi 2
: - Bye 3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.