I don't know what happened with org-babel
and Python recently. My intuition and my experience with preparing lectures last year was that the :session
option would cause Org Mode to capture all the output, i.e., the result of expressions in the block.
This year I started working on the same org file with an updated version of Emacs (26.1) and Org Mode (9.1.9). As you can see in my evaluation I only get the greeting messages and the output of print
.
What happened to the values of the other expressions?
#+BEGIN_SRC elisp
(setq org-babel-python-command "/usr/local/bin/python")
(org-babel-do-load-languages
'org-babel-load-languages
'(
(emacs-lisp . t)
(python . t)
))
#+END_SRC
#+RESULTS:
#+BEGIN_SRC python :session :results output
import sys
print(sys.version)
4
print(3)
#+END_SRC
#+RESULTS:
: Python 2.7.15 (default, Jun 17 2018, 13:05:56)
: [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)] on darwin
: Type "help", "copyright", "credits" or "license" for more information.
: 2.7.15 (default, Jun 17 2018, 13:05:56)
: [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.39.2)]
: 3
: python.el: native completion setup loaded
I was expecting 4
to be in the output.
#+BEGIN_SRC python :session :results output
4-2
print(4+5)
#+END_SRC
#+RESULTS:
: 9
I was expecting 2
to be in the output.
Note: Setting :results
to values
would only print the last value of the function. My goal is to have exactly the same output as in the interactive interpreter: the values of all typed expressions and of all print statements.
Update: difference between Org 8 and 9
I did further testing. The following two files are actually the same org file but the result sections have been evaluated, respectively, with Org 8 and Org 9.
On Org 8 the following code prints all intermediate values:
#+BEGIN_SRC python :session :results output
4
print(5)
6
'bye bye'
print('bye bye')
#+END_SRC
#+RESULTS:
: 4
: 5
: 6
: 'bye bye'
: bye bye
On Org 9 it only prints the output of the program:
#+BEGIN_SRC python :session :results output
4
print(5)
6
'bye bye'
print('bye bye')
#+END_SRC
#+RESULTS:
: 5
: bye bye
Furthermore on Org 8 the evaluation does not stop at the error,
and furthermore Python sees the code as coming from <stdin>
:
#+BEGIN_SRC python :session :results output
1
print(2)
3
4/0
5
print(6)
#+END_SRC
#+RESULTS:
: 1
: 2
: 3
: Traceback (most recent call last):
: File "<stdin>", line 1, in <module>
: ZeroDivisionError: division by zero
: 5
: 6
In Org 9 the code stops at the first error, and Python sees the code as coming from a temporary file.
#+BEGIN_SRC python :session :results output
1
print(2)
3
4/0
5
print(6)
#+END_SRC
#+RESULTS:
: 2
: Traceback (most recent call last):
: File "<stdin>", line 1, in <module>
: File "/var/folders/kf/p7km5ptj1p52hvz5nl6j9gr80000gn/T/babel-Jbpwl4/python-oz33CG", line 4, in <module>
: 4/0
: ZeroDivisionError: division by zero
The behavior I was looking for was the one of my old version of Emacs which came with Org 8 instead of 9.
Apparently the mechanism of Python session evaluation has been changed from Org 8 to Org 9.