[英]Python Sphinx Autosummary: failed to import module
I am trying to create autosummary using sphinx-autosummary for my python code which looks like as follows:我正在尝试使用 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
As mentioned in Sphinx documentation, I inserted the abspath of my working directory, added sphinx.ext.autodoc
to the list of extensions, and set autosummary_generate
to True in conf.py
.如 Sphinx 文档中所述,我插入了工作目录的绝对路径,将sphinx.ext.autodoc
添加到扩展列表中,并在conf.py
autosummary_generate
为 True。
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
Next, within docs/index.rst
, I added a reference to the refs/
folder.接下来,在docs/index.rst
中,我添加了对refs/
文件夹的引用。
.. toctree::
:maxdepth: 2
refs/index
The refs/index.rst
has reference to modA.rst
and modB.rst
. refs/index.rst
引用了modA.rst
和modB.rst
。
.. toctree::
:maxdepth: 2
modA
modB
In modA.rst
and modB.rst
, I am trying to create autosummaries.在modA.rst
和modB.rst
中,我正在尝试创建自动摘要。
modA.rst
Attributes
~~~~~~~~~~
.. autosummary::
:toctree: _autosummary
modA.modA.create_job
modB.rst
Attributes
~~~~~~~~~~
.. autosummary::
:toctree: _autosummary
modB.modB.get_jobs
While the code is working for modA.rst
, it fails for modB.rst
.虽然代码适用于modA.rst
,但对于modB.rst
却失败了。 The error says,错误说,
failed to import 'modB.modB.get_jobs': no module named modB.modB.get_jobs
I tried putting .. currentmodule::modB
and .. currentmodule::main
before the autosummary, but with no success.我尝试将.. currentmodule::modB
和.. currentmodule::main
放在自动摘要之前,但没有成功。 I even tried putting .. module::modB
and .. module::main
before autosummary, but even that is not working.我什至尝试将.. module::modB
和.. module::main
放在自动摘要之前,但即使这样也不起作用。 I searched a lot on the internet, but unable to understand why it's not working.我在互联网上搜索了很多,但无法理解为什么它不起作用。
Edit-1 : Added __init__.py
in both the folders. Edit-1 :在两个文件夹中添加了__init__.py
。
I had a similar issue.我有一个类似的问题。
My reason of failed import error was that the module modA.modA
, specified under .. autosummary::
directive, had imports of not installed libraries.我导入错误失败的原因是在.. autosummary::
指令下指定的模块modA.modA
导入了未安装的库。
Attributes
~~~~~~~~~~
.. autosummary::
:toctree: _autosummary
modA.modA.create_job
But in general your reason for failed to import
error can be different, because the function that imports modules from .. autosummary::
directive catches all errors and autosummary prints to output the same error message.但总的来说,您failed to import
的原因可能会有所不同,因为从.. autosummary::
指令导入模块的 function 会捕获所有错误,并且自动摘要会打印到 output 相同的错误消息。
Here is the related issue on github https://github.com/sphinx-doc/sphinx/issues/7989这是 github https 上的相关问题://github.com/sphinx-doc/sphinx/issues/7989
I suggest to import your modules modA.modA
, modB.modB
and see if there are any errors during importing.我建议导入您的模块modA.modA
, modB.modB
并查看导入过程中是否有任何错误。
I had similar issue in using Sphinx.我在使用 Sphinx 时遇到了类似的问题。
After some experiments, I suspect that the error message like failed to import XXX: no module named XXX
does not necessarily mean the module XXX cannot be found.经过一些实验,我怀疑像failed to import XXX: no module named XXX
之类的错误信息并不一定意味着找不到模块 XXX。 Instead, in my experiment, there is some error in importing XXX.相反,在我的实验中,导入XXX时出现了一些错误。
In my case, the module XXX itself is importing some package YYY that has not been installed.就我而言,模块 XXX 本身正在导入一些尚未安装的 package YYY。 Since Sphinx import XXX in order to extract docstring, it found an error in this import attempt.由于 Sphinx import XXX 是为了提取 docstring,因此在此导入尝试中发现了错误。 After I removed that import YYY
statement, I can successfully build the documentation.删除该import YYY
语句后,我可以成功构建文档。
I have the same problem with a large code base and it seems that when Sphinx can't handle an import statement it will just show some generic module import warning.我对大型代码库也有同样的问题,似乎当 Sphinx 无法处理导入语句时,它只会显示一些通用模块导入警告。 Even when the package seems to be installed such warnings can occure.即使似乎安装了 package,也会出现此类警告。
The only workaround I found so far is using the autodoc_mock_imports in the conf.py for some specific libraries to mock specific libraries that Sphinx can't handle correctly.到目前为止,我发现的唯一解决方法是对某些特定库使用 conf.py 中的 autodoc_mock_imports 来模拟 Sphinx 无法正确处理的特定库。
Example: autodoc_mock_imports = ["flask", "flask_restx", "mongoengine", "model", "flask_jwt_extended"]示例:autodoc_mock_imports = ["flask", "flask_restx", "mongoengine", "model", "flask_jwt_extended"]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.