4

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.

Aaron Hall
  • 419
  • 1
  • 6
  • 20
MassimoLauria
  • 200
  • 1
  • 8
  • I don't believe there's an option to get *all* the values, just the last value that would be echoed on the Python shell (which excludes `None`). – Aaron Hall Oct 03 '18 at 01:31
  • It seems that the behavior I was expecting was indeed the behavior of org-mode 8, and changed along the way. – MassimoLauria Oct 04 '18 at 07:26

1 Answers1

3

As you said in a comment:

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.

You can't have both, but if you want "exactly the same output as in an interactive interpreter" from multiple lines, you can get that by printing the results of the expression from the repr function if the value is not None.

So here's a convenience function for that, output (indented because I edited it in a separate python-mode buffer, accessed with C-c '):

#+BEGIN_SRC python :session :results output
  def output(obj):
      if obj is None:
          return None
      print(repr(obj))


  import sys
  print(sys.version)
  output(sys.version)
  output("hello")
  output(4-1)
  output(list("hello"))

#+END_SRC

#+RESULTS:
: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) 
: [GCC 7.2.0]
: '3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) \n[GCC 7.2.0]'
: 'hello'
: 3
: ['h', 'e', 'l', 'l', 'o']
: 
:

Note that strings will have their quotes and literal newlines, while numbers will also look like their literals. This is how the interactive interpreter works.

But I don't think it's very elegant in this context, and as people have been writing Python in Emacs for a long time, I doubt we can come up with a better approach.

Explanation

In a code block with :results output it takes the output from standard out, that is, what you print, as the results. This is what you were doing:

#+BEGIN_SRC python :session :results output
import sys
print(sys.version)
#+END_SRC

#+RESULTS:
: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) 
: [GCC 7.2.0]

If you don't print (or write to standard out) with output, you get nothing:

#+BEGIN_SRC python :session :results output
import sys
sys.version
#+END_SRC

#+RESULTS:

Orgmode will show the last value when you use session, when you use the :results value (which you can actually leave out because it is the default):

#+BEGIN_SRC python :session :results value
import sys
sys.version
#+END_SRC

#+RESULTS:
: 3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) 
: [GCC 7.2.0]

Or you can specify suboptions for value:

#+BEGIN_SRC python :session :results value raw
import sys
sys.version
#+END_SRC

#+RESULTS:
3.6.4 |Anaconda custom (64-bit)| (default, Mar 13 2018, 01:15:57) 
[GCC 7.2.0]

So if you remove the argument, you get what you were actually expecting (since print() returns None):

#+BEGIN_SRC python :session 
4-2
print(4+5)
#+END_SRC

#+RESULTS:
: 2

From orgmode's docs

:results {output, value}: Value mode is the default (as with other languages). In value mode you can use the following subtypes:

  • raw: value is inserted directly
  • pp: value is pretty-printed by python using pprint.pformat(%s), then inserted
  • file: value is interpreted as a filename to be interpolated when exporting; commonly used for graphics output.
Aaron Hall
  • 419
  • 1
  • 6
  • 20
  • For some reasons I was convinced that at some point in the past having `:session` and `:results output` would put in the result block all the text you would get back during the interactive session. For example if the code was `1;print(2);3;print(4)` the result would be`1\n\2\n\3\n4\n`. Was this ever the behavior of `org-babel` at some point? – MassimoLauria Oct 03 '18 at 07:04
  • @MassimoLauria Not to my knowledge, and I've been using it for a few years (probably five) now, and I would not expect it to have that behavior going forward. If you want multiple values, either use print to collect output, or have a final tuple of values. – Aaron Hall Oct 03 '18 at 15:05
  • @MassimoLauria I have updated my answer with a bit of a workaround that addresses what you said in a comment. If it answers your question, you can accept it with the checkmark, which will give you +2 to your rep. – Aaron Hall Oct 03 '18 at 19:53