python - 交叉引用链接到不同子模块中的函数/类

标签 python python-sphinx cross-reference

作为一个简单的例子,在以下2个子模块的代码中(a.pyb.py在同一目录)。指向同一子模块函数 :func:`hook` 的链接有效,但指向不同模块的链接交叉引用无效,即 :func:`foo`。我还尝试了 :func:`.a.foo` 的语法 - 仍然无效。如何交叉引用 a.foo()

# script a.py
def foo():
    '''foo func'''

# script b.py
def hook():
    '''hook func'''

def spam():
    '''spam func.
    :func:`foo`
    :func:`hook`
'''

最佳答案

docs 中所述:

Normally, names in these roles are searched first without any further qualification, then with the current module name prepended, then with the current module and class name (if any) prepended. If you prefix the name with a dot, this order is reversed.

在这种情况下,:func:`.a.foo` 表示模块 b 中名为 a 的对象。它将寻找 b.a.foo 函数。

你应该试试 :func:`..a.foo`,它会指向 b..a.foo,或者只是 a.foo (现在无法在本地检查,抱歉;但我记得我以前使用过该语法)。

但请注意,a.pyb.py 应该是模块,即可以在它们的名称下导入。如果它们只是脚本,并且不在包中(没有 __init__.py 文件到项目的根目录),则无法交叉引用这些角色。

你可以尝试使用:any:角色 — :any:`foo` — 并希望它能在所描述对象的通用索引中找到该对象。

关于python - 交叉引用链接到不同子模块中的函数/类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46622995/

相关文章:

python - 如何将值从一个 python 脚本返回到另一个?

java - 调用 ScopeProvider 中引用的对象时出现错误 "Cyclic linking detected"

python - 从python中的dict更新数据类字段

python - 渲染列表对象时出现问题 : AttributeError: 'list' object has no attribute '...'

python - Matplotlib:颜色图可以暗示不同的默认规范化吗?

python - pylab matplotlib "show"等待窗口关闭

python-3.x - Sphinx 和 Markdown .md 链接

python - 狮身人面像自动摘要 "toctree contains reference to nonexisting document"警告

python-sphinx - 在 Sphinx 中包含另一个页面的 ToC

github - github 存储库的交叉引用器