Sphinx Program Output Extension
sphinxcontrib-programoutput is a Sphinx extension that enables the literal insertion of arbitrary command output into documentation. This helps maintain up-to-date command examples. The current version is 0.19, with releases occurring on an irregular but active cadence, most recently in February 2026.
Common errors
-
ERROR: Unknown directive type "program-output"
cause The `sphinxcontrib-programoutput` extension has not been correctly added to the `extensions` list in your Sphinx `conf.py` file. Sphinx cannot find or recognize the directive.fixEdit your `conf.py` file and add `'sphinxcontrib.programoutput'` to the `extensions` list: `extensions = ['sphinxcontrib.programoutput', ...]`. Make sure there are no typos. -
ERROR: Command 'my_command' failed: [Errno 2] No such file or directory
cause The command specified in `.. program-output::` or `.. command-output::` cannot be found in the PATH of the environment where Sphinx is being built, or the `cwd` option is pointing to an incorrect directory, or the command itself is not executable. This often occurs when building documentation in a different environment (e.g., CI/CD, ReadTheDocs) than local development.fixEnsure the command is installed and available in the execution environment's PATH. If it's a script from your project, ensure `cwd` is set correctly to the directory containing the script, or provide the full path to the executable. Verify Python virtual environments are correctly activated during the Sphinx build process. -
ImportError: No module named 'pbr.version'
cause This specific `ImportError` (or similar for other modules) within `conf.py` when using `sphinxcontrib-programoutput` often indicates that Sphinx is not being run within the correct Python virtual environment that contains all project dependencies, including those needed for `conf.py` itself or by `programoutput`'s execution.fixActivate your project's virtual environment before running `sphinx-build`. For automated builds (CI/CD, ReadTheDocs), ensure the build process correctly activates the environment or installs all necessary dependencies.
Warnings
- breaking Version 0.18 significantly updated its Python and Sphinx requirements. It dropped support for Python versions older than 3.8 and now effectively requires Sphinx 5.0+ and docutils 0.18.1+. Projects on older Python or Sphinx versions will need to upgrade or stick to programoutput < 0.18.
- gotcha By default, `sphinxcontrib-programoutput` does not interpret ANSI escape codes for coloring. To enable this, you must explicitly enable the `sphinxcontrib.ansi` extension, ensure you are on Python 3.10+, and set the `programoutput_use_ansi` configuration value to `True`.
- gotcha Commands are executed in the top-level source directory by default. When using the `cwd` option, a relative path is interpreted relative to the *current source file*, while an absolute path is relative to the *root of the source directory*. Misunderstanding this can lead to 'file not found' errors.
- gotcha If a command returns a non-zero exit code, `sphinxcontrib-programoutput` by default emits a build warning. If you intend a command to fail (e.g., demonstrating error handling), this warning can be suppressed or inverted using the `:returncode:` option.
Install
-
pip install sphinxcontrib-programoutput
Imports
- sphinxcontrib.programoutput
import sphinxcontrib.programoutput
extensions = ['sphinxcontrib.programoutput'] # in conf.py
Quickstart
# conf.py
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinxcontrib.programoutput'
]
# Your_Document.rst
.. program-output:: python -V
:shell:
.. command-output:: python -c "print('Hello from Python!')"
# Example with options
.. program-output:: git log -1
:cwd: .
:ellipsis: 2,-1