简体   繁体   English

这个 Sphinx autodoc MockFinder 错误的原因是什么?

[英]What is the cause of this Sphinx autodoc MockFinder error?

I am creating documentation with Sphinx.我正在使用 Sphinx 创建文档。 My folder structure looks as follows:我的文件夹结构如下:

MyProject
├── mypackage
│   ├── __init__.py
│   ├── mycode.py
│   └── etc.
└── docs
    ├── build
    ├── make.bat
    ├── Makefile
    └── source
       ├── conf.py
       ├── index.rst
       ├── _static
       └── _templates

I begin by running make clean and make html in the docs directory.我首先在docs目录中运行make cleanmake html Next, to populate the documentation, I run sphinx-apidoc -o ./source ../mypackage , and all corresponding .rst files are generated as expected.接下来,为了填充文档,我运行sphinx-apidoc -o ./source ../mypackage ,并按预期生成所有相应的.rst文件。 Finally, I run make clean and make html once more to ensure a clean build, as suggested in the Sphinx-RTD-Tutorial .最后,按照Sphinx-RTD-Tutorial中的建议,我再次运行make cleanmake html以确保构建干净。 However, on this final build, I get the following output:但是,在这个最终版本中,我得到以下输出:

Running Sphinx v4.0.2
making output directory... done
[autosummary] generating autosummary for: index.rst, mypackage.rst, mypackage.mycode.rst

Extension error (sphinx.ext.autosummary):
Handler <function process_generate_options at 0x10678dee0> for event 'builder-inited' threw an exception (exception: list.remove(x): x not in list)
make: *** [html] Error 2

Removing the autosummary extension and just running autodoc with the same sequence of commands leads to a similar error:删除 autosummary 扩展并仅使用相同的命令序列运行 autodoc 会导致类似的错误:

Exception occurred:
  File "/Users/myname/opt/anaconda3/envs/myenv/lib/python3.9/site-packages/sphinx/ext/autodoc/mock.py", line 151, in mock
    sys.meta_path.remove(finder)
ValueError: list.remove(x): x not in list

Here is the source code method that the error comes from:这是错误来自的源代码方法:

@contextlib.contextmanager
def mock(modnames: List[str]) -> Generator[None, None, None]:
    """Insert mock modules during context::

        with mock(['target.module.name']):
            # mock modules are enabled here
            ...
    """
    try:
        finder = MockFinder(modnames)
        sys.meta_path.insert(0, finder)
        yield
    finally:
        sys.meta_path.remove(finder)
        finder.invalidate_caches()

Does anyone know what might be raising this error or have an idea as to what is happening in this method?有谁知道什么可能会引发这个错误或知道这种方法中发生了什么? Could it have to do with my specification of sys.path in my conf.py file?它可能与我在conf.py文件中对sys.path规范有关吗?

[conf.py]

sys.path.insert(0, os.path.abspath('../../mypackage'))

I was able to resolve this error using the autodoc_mock_imports config:我能够使用autodoc_mock_imports配置解决此错误:

autodoc_mock_imports autodoc_mock_imports
This value contains a list of modules to be mocked up.该值包含要模拟的模块列表。 This is useful when some external dependencies are not met at build time and break the building process.当在构建时不满足某些外部依赖并中断构建过程时,这很有用。 You may only specify the root package of the dependencies themselves and omit the sub-modules:您只能指定依赖项本身的根包并省略子模块:

 autodoc_mock_imports = ["django"]

Will mock all imports under the django package.将模拟 django 包下的所有导入。

New in version 1.3. 1.3 版中的新功能。

Changed in version 1.6: This config value only requires to declare the top-level modules that should be mocked.在 1.6 版更改:此配置值只需要声明应该模拟的顶级模块。

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

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