26

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?

2 Answers 2

26

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.

24

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
  • 4
    This is not a hack. :)
    – Omar
    Commented Oct 4, 2018 at 2:42
  • While these examples directly worked, I have also upvoted the @user2699 answer. It is necessary to look to the explanations, not to just repeat a magic. Thanks guys.
    – jaromrax
    Commented Jan 25, 2021 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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