Running Python and R inside Emacs

Emacs org-mode lets you manage blocks of source code inside a text file. You can execute these blocks and have the output display in your text file. Or you could export the file, say to HTML or PDF, and show the code and/or the results of executing the code.

Here I’ll show some of the most basic possibilities. For much more information, see And for the use of org-mode in research, see A Multi-Language Computing Environment for Literate Programming and Reproducible Research.

Source code blocks go between lines of the form


On the #+begin_src line, specify the programming language. Here I’ll demonstrate Python and R, but org-mode currently supports C++, Java, Perl, etc. for a total of 35 languages.

Suppose we want to compute √42 using R.

#+begin_src R

If we put the cursor somewhere in the code block and type C-c C-c, org-mode will add these lines:

: 6.48074069840786

Now suppose we do the same with Python:

#+begin_src python
from math import sqrt

This time we get disappointing results:

: None

What happened? The org-mode manual explains:

… code should be written as if it were the body of such a function. In particular, note that Python does not automatically return a value from a function unless a return statement is present, and so a ‘return’ statement will usually be required in Python.

If we change sqrt(42) to return sqrt(42) then we get the same result that we got when using R.

By default, evaluating a block of code returns a single result. If you want to see the output as if you were interactively using Python from the REPL, you can add :results output :session following the language name.

#+begin_src python :results output :session
print "There are %d hours in a week." % (7*24)

This produces the lines

: There are 168 hours in a week.
: 1024

Without the :session tag, the second line would not appear because there was no print statement.

I had to do a couple things before I could get the examples above to work. First, I had to upgrade org-mode. The version of org-mode that shipped with Emacs 23.3 was quite out of date. Second, the only language you can run by default is Emacs Lisp. You have to turn on support for other languages in your .emacs file. Here’s the code to turn on support for Python and R.

    'org-babel-load-languages '((python . t) (R . t)))

Update: My next post shows how to call code in written in one language from code written in another language.

Related posts:

Personal organization software
Preventing an unpleasant Sweave surprise

Tagged with: , , , ,
Posted in Python
8 comments on “Running Python and R inside Emacs
  1. seamus says:

    Emacs has ESS for having a proper R terminal session inside it. There is also Sweave for generating PDF reports with R and LaTeX.

  2. John says:

    Sweave may be more convenient if you’re only interested in R, depending on what you’re wanting to do. But what I find most interesting about org-mode is its multi-lingual support.

  3. John Johnson says:

    There’s a lot to like about org-mode. Sweave is nice if you are interested in embedding R in LaTeX, but if you’d rather compose in something more lightweight and use different languages, org-mode is very nice. I also like that you can preview the org-mode stuff before producing it.

  4. zerology says:

    Thank you!

    Can you tell, how I inform emacs/org about the place of my python executable?

    Thanks, z.

  5. John says:

    zerology: I believe it looks at your path to find each language’s compiler or interpreter.

  6. zerology says:

    Thank you again. That did it.

  7. org-mode should be able to execute blocks of source of any valid Python code.
    Your example sqrt(42) should work right from the spot. It’s a bug in org-mode. Explanation so far leads astray.



  8. Robin Green says:

    The customization for the python command is in customization group “Python” under “Python Python Command”, but org.mode tells me that only python version >=2.2 and <3.0 are supported.

2 Pings/Trackbacks for "Running Python and R inside Emacs"
  1. [...] Running Python and R inside Emacs — The Endeavour. Like this:LikeBe the first to like this post. [...]

  2. [...] Sweave Running Python and R inside Emacs Literate programming in [...]