Introduction

Python is a high-level, readable, interpreted language which can be
used for many common computing tasks. It runs on most modern
operating systems. Python source code blocks are fully supported in
Org Mode with a wide variety of Python-specific header arguments.

Python source code blocks in Org Mode can be used to define functions,
filter and analyze data, create graphics and figures, and produce
reproducible research papers.

Requirements and Setup

Python source code blocks in Org Mode require a working python installation.
Python is included in Mac OS X and often in Gnu/Linux, and is easily
available for Windows. Python installers are located at
the Python download site.

To configure your emacs org-mode to use python, you'll need to ensure
that org-babel-load-languages includes an entry for it.
Typically, org-babel-load-languages will contain many entries. The
example below omits other languages.

Sessions

Session mode is fully supported in python, including named sessions.
In session mode, each block is run in the same long-running python
interactive interpreter session, as if you had typed that block into
python. You can have multiple sessions, all independent.

Sessions can be used to define functions, set up variables, and share
code between source blocks.

Session mode in python is slightly different from non-session mode,
because in session mode you are talking to a single "interactive"
python session. In python's interactive mode, blank lines are
special: they indicate the end of an indented block. So you have to
write your org-mode python a little differently when using session
mode.

Also, in non-session mode, the python code block will be wrapped in a
function, so to return a value (in :results value mode) you have to
use a return statement. In session mode, the python code is
evaluated directly by the interpreter, not in a function context, and
the last statement's value will be automatically returned, so you must
not use a return statement.

Finally, if you are using matplotlib for graphics, matplotlib uses an
"interactive" backend when started from an interactive python (as you
might expect). So you have to set the backend explicitly to a PDF or
PNG or other file-exporting backend when using session mode. See the
example at