Question

I want to write a book using Sphinx and restructured text. As most of the content will be solutions written in Python I want to separate the text and the code.

Is there a command to add external python scripts to my restructured text documents?

I tried to use

.. code-block:: python
    .. include:: unittest_exp1.py

and

.. code-block:: python
    .. file:: unittest_exp1.py
Was it helpful?

Solution

Use literalinclude as shown in the Sphinx documentation.

.. literalinclude:: example.py
   :language: python
   :emphasize-lines: 12,15-18
   :linenos:

OTHER TIPS

Have you try .. literalinclude:: filename?

Here is a little example:

.. literalinclude:: filename
    :linenos:
    :language: python
    :lines: 1, 7-8 
    :start-after: 12
    :end-before: 5
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top