gpt4 book ai didi

Python Sphinx 自动摘要 : failed to import module

转载 作者:行者123 更新时间:2023-12-04 14:13:51 26 4
gpt4 key购买 nike

我正在尝试使用 sphinx-autosummary 为我的 python 代码创建自动摘要,如下所示:

main
├───modA
| ├───__init__.py
| ├───modA.py
├───modB
| ├───__init__.py
| ├───modB.py
├───docs
| ├───build
| └───source
| ├───refs
| | |───_autosummary
| | |───index.rst
| | |───modA.rst
| | |───modB.rst
| ├───index.rst
| ├───conf.py

正如 Sphinx 文档中提到的,我插入了我的工作目录的 abspath,添加了 sphinx.ext.autodoc到扩展列表,并设置 autosummary_generateconf.py 中为真.
import os
import sys
sys.path.insert(0, os.path.abspath('../..'))

# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
"sphinx.ext.autosummary",
'sphinx.ext.coverage',
'sphinx.ext.napoleon'
]

autosummary_generate = True

接下来,在 docs/index.rst内,我添加了对 refs/的引用文件夹。
.. toctree::
:maxdepth: 2

refs/index
refs/index.rst引用了 modA.rstmodB.rst .
.. toctree::
:maxdepth: 2

modA
modB

modA.rstmodB.rst ,我正在尝试创建自动摘要。
modA.rst
Attributes
~~~~~~~~~~
.. autosummary::
:toctree: _autosummary

modA.modA.create_job
modB.rst
Attributes
~~~~~~~~~~
.. autosummary::
:toctree: _autosummary

modB.modB.get_jobs

虽然代码适用于 modA.rst ,它失败了 modB.rst .错误说,
failed to import 'modB.modB.get_jobs': no module named modB.modB.get_jobs

我试过把 .. currentmodule::modB.. currentmodule::main在自动摘要之前,但没有成功。我什至试过把 .. module::modB.. module::main在自动摘要之前,但即使这样也不起作用。我在互联网上搜索了很多,但无法理解为什么它不起作用。

Edit-1 : 添加 __init__.py在两个文件夹中。

最佳答案

我有一个类似的问题。
我导入失败错误的原因是模块 modA.modA ,在 .. autosummary:: 下指定指令,导入了未安装的库。

Attributes
~~~~~~~~~~
.. autosummary::
:toctree: _autosummary

modA.modA.create_job
但总的来说你的原因 failed to import错误可能不同,因为从 .. autosummary:: 导入模块的函数指令捕获所有错误并自动摘要打印以输出相同的错误消息。
这是github上的相关问题 https://github.com/sphinx-doc/sphinx/issues/7989
我建议导入你的模块 modA.modA , modB.modB并查看导入过程中是否有任何错误。

关于Python Sphinx 自动摘要 : failed to import module,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62226506/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com