Jon
Jon

Reputation: 12874

How can I add an external Python file to my restructured text document?

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

Upvotes: 4

Views: 658

Answers (2)

G M
G M

Reputation: 22459

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

Upvotes: 3

user2776629
user2776629

Reputation:

Use literalinclude as shown in the Sphinx documentation.

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

Upvotes: 5

Related Questions