Pergunta

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
Foi útil?

Solução

Use literalinclude as shown in the Sphinx documentation.

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

Outras dicas

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
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top