13

This is my source block:

#+BEGIN_SRC python
print "hello"
#+END_SRC

The output is always:

#+RESULTS:
: None

This is my org-babel configuration:

    (org-babel-do-load-languages
 'org-babel-load-languages
 '((python . t)
   (C . t)
   (calc . t)
   (latex . t)
   (java . t)
   (ruby . t)
   (scheme . t)
   (sh . t)
   (sqlite . t)
   (js . t)))

Python is on my PATH and executable via terminal.

Emacs-lisp SRC blocks works just fine.

Why do I get None instead of hello?

15

This is a peculiarity of how babel handles some languages. This gives some detail on python, and a complete list of options is available here. There are quite a few useful ones.

Basically, depending on the language, there are several options for output. Sometimes it makes sense to use the standard output (what print does), for other languages (eg octave), it makes sense to display an image. For python, the default is value, which shows the return value of the code, so changing your print statement to a return would give the expected behavior.

This isn't very useful in some cases, so it can be changed by adding :results output to the first line of the source block.

12

In a very simple case like this one, there's a hack I tend to use, replace print with return:

#+BEGIN_SRC python
return "hello"
#+END_SRC

#+RESULTS:
: hello

But in general the better solution will be to use :results output, as mentioned by user2699.

#+BEGIN_SRC python :results output
print("hello")
#+END_SRC

#+RESULTS:
: hello
  • 2
    This is not a hack. :) – Omar Oct 4 '18 at 2:42

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.