简体   繁体   English

Sphinx autodoc 文档 namespace.module 可以不记录命名空间吗?

[英]Can Sphinx autodoc document namespace.module without documenting namespace?

The code base I'm working with is structured under a namespace .我正在使用的代码库是在namespace下构建的。 I'm responsible for namespace.my_project , but I depend on various other modules from namespace .我负责namespace.my_project ,但我依赖于namespace各种其他模块。 Is there any way to tell Sphinx (with autodoc) to document namespace.my_project , but ignore everything directly under namespace ?有什么方法可以告诉 Sphinx(使用 autodoc)记录namespace.my_project ,但直接忽略namespace下的所有内容?

I've tried adding exactly the module I want to index.rst :我试过准确添加我想要index.rst的模块:

.. toctree::
    namespace.my_project

This shows up fine in the final documentation, but during compilation I still get a lot of warnings from members of namespace that I don't have control over.这在最终文档中显示得很好,但在编译过程中,我仍然从namespace成员那里收到很多我无法控制的警告。

There aren't many options for this, using the autodoc extension it imports your objects with the normal Python importing rules.对此没有太多选择,使用 autodoc 扩展,它使用普通的 Python 导入规则导入您的对象。

sphinx.ext.autodoc – Include documentation from docstrings sphinx.ext.autodoc – 包含文档字符串中的文档

This extension can import the modules you are documenting, and pull in documentation from docstrings in a semi-automatic way.这个扩展可以导入你正在记录的模块,并以半自动的方式从文档字符串中提取文档。

So, you would have to selectivly write your .rst files using autodoc directives that import only what you want.因此,您必须使用仅导入您想要的内容的 autodoc 指令有选择地编写.rst文件。 From your example, assuming my_project is structured as a package, this could be relativly easy, you would write:从您的示例中,假设my_project被构造为一个包,这可能相对容易,您可以编写:

.. automodule:: namespace.my_project

or lets say you have 2 different packages within my_project或者假设您在my_project有 2 个不同的包

.. automodule:: namespace.my_project.my_package1

.. automodule:: namespace.my_project.my_package2

Basically you've imported only the modules/objects from the project that interest you.基本上,您只从您感兴趣的项目中导入了模块/对象。 Then in the .. toctree:: directive you would include only .rst files that have these carefully chosen directives.然后在.. toctree::指令中,您将只包含具有这些精心选择的指令的.rst文件。

This does have a few drawbacks however, all the objects in the namespace that you don't explicitly document would not be included in the index and would not be cross-referenceable (unless you created empty .rst declarations with domain directives ).但是,这确实有一些缺点,您未明确记录的名称空间中的所有对象都不会包含在索引中,也不会交叉引用(除非您使用域指令创建了空的.rst声明)。 They would, however, be rendered textually without links and assuming there are no underlying Python errors the documentation would build normally without warnings.然而,它们将在没有链接的情况下以文本形式呈现,并且假设没有潜在的 Python 错误,文档将正常构建而不会发出警告。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM