@@ -111,3 +111,74 @@ And finally, this is how the result would look like:
111111 HTML result of documenting a Python function in Sphinx with cross-references
112112
113113Beautiful, isn't it?
114+
115+ Including doctests in your documentation
116+ ----------------------------------------
117+
118+ Since you are now describing code from a Python library, it will become useful
119+ to keep both the documentation and the code as synchronized as possible.
120+ One of the ways to do that in Sphinx is to include code snippets in the
121+ documentation, called *doctests *, that are executed when the documentation is
122+ built.
123+
124+ To demonstrate doctests and other Sphinx features covered in this tutorial,
125+ you will need to setup some basic Python infrastructure first.
126+
127+ Preparing the Python library
128+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129+
130+ Begin by activating the virtual environment (as seen in the :ref: `getting
131+ started <tutorial-getting-started>` section of the tutorial) and install
132+ `flit <https://pypi.org/project/flit/ >`_ on it:
133+
134+ .. code-block :: console
135+
136+ $ source .venv/bin/activate
137+ (.venv) $ python -m pip install "flit~=3.3"
138+
139+ Next, create two files on the same level as ``README.rst ``: ``pyproject.toml ``
140+ and ``lumache.py ``, with these contents:
141+
142+ .. code-block :: toml
143+ :caption: pyproject.toml
144+
145+ [build-system]
146+ requires = ["flit_core >=3.2,<4"]
147+ build-backend = "flit_core.buildapi"
148+
149+ [project]
150+ name = "lumache"
151+ authors = [{name = "Graziella", email = "graziella@lumache"}]
152+ dynamic = ["version", "description"]
153+
154+ .. code-block :: python
155+ :caption: lumache.py
156+
157+ """
158+ Lumache - Python library for cooks and food lovers.
159+ """
160+
161+ __version__ = " 0.1.0"
162+
163+ And finally, install your own code and check that importing it works:
164+
165+ .. code-block :: console
166+
167+ (.venv) $ flit install --symlink
168+ ...
169+ (.venv) $ python -c 'import lumache; print("OK!")'
170+ OK!
171+
172+ Congratulations! You have created a basic Python library.
173+
174+ .. note ::
175+
176+ The ``pyproject.toml `` file you created above is required so that
177+ your library can be installed. On the other hand,
178+ ``flit install --symlink `` is an alternative to ``pip install . ``
179+ that removes the need to reinstall the library every time you make
180+ a change, which is convenient.
181+
182+ An alternative is to not create ``pyproject.toml `` at all,
183+ and setting the :envvar: `PYTHONPATH `, :py:data: `sys.path `, or
184+ equivalent. However, the ``pyproject.toml `` approach is more robust.
0 commit comments