在Sphinx文档中,是否可以使用模块名称来指定要包含的代码示例?

发布于 2024-11-14 02:32:50 字数 427 浏览 5 评论 0原文

我想在我的 sphinx 文档中包含一个代码示例。 但是,我不想通过文件的路径来引用该文件, 但宁愿通过其模块名称来指定它。

也就是说,不要按照此处指示的方式显示代码示例

.. literalinclude example.py

或者

.. literalinclude ../../example.py

我宁愿do(例如)

.. includemodule mymodule.example

是否可以在不编写自定义扩展的情况下完成此操作? 或者,编写此类扩展的最简单方法是什么?

I'd like to include a code example in my sphinx documentation.
I don't, however, want to refer to the file by its path,
but would rather specify it by its module name.

That is, instead of doing it the way indicated here Showing code examples

.. literalinclude example.py

or

.. literalinclude ../../example.py

I'd rather do (for example)

.. includemodule mymodule.example

Is this possible to do without writing a custom extension?
Or, what is the simplest way to write such an extension?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绮烟 2024-11-21 02:32:50

您可以使用如下方式引用其他类:

:class:`~module.submodule.Class`

如果您将示例作为 doctest 添加到类中,那么您将拥有保证可以工作的示例,因为它们可以直接运行。

You can reference other classes by using something like this:

:class:`~module.submodule.Class`

If you add the examples as doctest to the class than you have examples which are guaranteed to work since they are directly runnable.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文