0

First question: How does one catch the results out of the second code block below?

#+begin_src python :session :results silent
  band = "ABBA"
#+end_src

#+begin_src python :session
  return band
#+end_src

#+RESULTS:

Running C-c C-c on both of them consecutively yields to empty results. However they both seem to run properly as there's no error for undefined variable band in the latter block. For example the code below gives results as expected.

#+begin_src python
  return "ABBA"
#+end_src

#+RESULTS:
: ABBA

Secondly, I recall reading somewhere in the documentation that the sessions can be named and run in isolated environments. How does that work? There seems to be no examples of such behaviour.

2

Unfortunately, session evaluation for python code blocks works differently from non-session evaluation and requires changes to the code blocks to make them do the same thing. AFAIK, python is unique among Babel languages in this respect.

For non-session evaluation, Org babel implicitly wraps the body of the block in a function: that's why you need to return the value. No such wrapping takes place for session evaluation, so adding a return generates an error - look in the *Python* buffer which is the default name of the session: you'll see that it says SyntaxError: 'return' outside function.

So for session evaluation, try these two blocks:

#+begin_src python :session :results silent
  band = "ABBA"
#+end_src

#+begin_src python :session
  band
#+end_src

#+RESULTS:
: ABBA

The value of the block is the value of the expression (here just a variable) on the last line of the block.

1

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.