Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Sphinx support for execution of python code from code blocks or files. Original code by JP Senior.

Project description


Sphinx-computron 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. The output is interpreted as ReST, as if the output of the executed code was copy-pasted in-place into the document.

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.

The original author is JP Senior. His version of the package appears to be unmantained so I decided to salvage it by making a hard fork. The name had to be changed to avoid collisions with his version published on PyPI.


Options are:

If specified, will load code from a file (relative to sphinx doc root) and prepend that to the directive’s content before its execution.


Executing python code and parsing output as ReST:

.. computron-injection::

    print('*This is interpreted as ReST!*')

Whatever is written into stdout is collected and the injected in-place into the document. Stderr is not captured.

Executing python code from a file

computron-injection also allows you to import a python file and execute it within a document. The path is specified relative to the source ReST file where the directive is encountered.

Running a Python file in the same directory:

.. computron-injection::

Activating on Sphinx

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

Activating the extension in sphinx:


Project details

Release history Release notifications

Download files

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

Files for sphinx-computron, version 0.1.0
Filename, size File type Python version Upload date Hashes
Filename, size sphinx_computron-0.1.0-py3-none-any.whl (4.4 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size sphinx_computron-0.1.0.tar.gz (3.1 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page