Skip to main content
Python Software Foundation 20th Year Anniversary Fundraiser  Donate today!

Sphinx support for execution of python code from code blocks or files

Project description


Sphinx-execute-code is an extension for Sphinx that allows a document author to insert arbitrary python code samples in code blocks, or run python code from python files on the filesystem.

This was written as an alternative to other code execution functions which relied on doctest formats, and attempts to be more flexible, similar to literal-block and code-block statements.


This module allows you to run arbitrary code, and should be treated with caution.


Options right now (as of version 0.2) are:

If specified, will show line numbers
Customizes pygments lexxer for specified language (Eg, Javascript, bash)
If specified, will hide the code block and only show results
If specified, hides the ‘Code’ and ‘Results’ caption headers around the literal blocks
If specified, will load code from a file (relative to sphinx doc root) and ignore content.


Running ‘execute_code’ as a directive allows the administrator to embed exact python code as if it was pasted in a normal code-block.

Executing python code and showing the result output:

.. execute_code::

    print 'python highlight code'

    class Foo(object):
        def __init__(self):
   = 'baz'
        def out(self):

    f = Foo()

Output language

Customizing the output syntax can be helpful to make it easy to document any other pygments lexxer - eg ini, javascript

We can customize the output language parser (for JSON/Javascript):

.. execute_code::
    :output_language: javascript

    print "'{foo-bar-baz}'"

Hiding code

You may want to hide the example code that is executing (avoiding highlighting/etc) and display the results only.

We can also hide the code input, showing only the executed code results:

.. execute_code::

    print 'This should not print the example code'

Suppressing output headers

Suppressing the ‘Headers’ outputs for Code and Results header:

.. execute_code::

   foo = 32
   print 'This will hide the Code and Results text - and foo is %d' % foo

Executing python code from a file

execute_code also allows you to import a python file and execute it within a document.

Running a Python file from filename from the .py example:

.. execute_code::
   :filename: tests/

This function also supports the argument ‘hide_filename’:

.. execute_code::
   :filename: tests/


Installation from source:

$ git clone
$ python install

Installation from pypi:

$ pip install sphinx-execute-code

Activating on Sphinx

To activate the extension, add it to your extensions variable in for your project.

Activating the extension in sphinx:


Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for sphinx_execute_code, version 0.2a2
Filename, size File type Python version Upload date Hashes
Filename, size sphinx_execute_code-0.2a2.tar.gz (6.6 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page