문제

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
도움이 되었습니까?

해결책

Use literalinclude as shown in the Sphinx documentation.

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

다른 팁

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
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top