首页 > 解决方案 > Sphinx 发出警告,因为它找不到导入的文件

问题描述

我正在尝试生成文档,但在命令行中遇到警告。我一直在搜索该站点,但找不到适合我的解决方案。我将原因缩小到 ClassB 中的导入。

我的文件夹结构是:

文件夹结构

__init__.py文件没有内容。

类A.rst:

classA
======

.. automodule:: app.ClassA
    :members:

类B.rst:

classB
======

.. automodule:: app.ClassB
    :members:

index.rst(仅限目录树):

.. toctree::
   :maxdepth: 2
   :caption: Contents:

   classA
   classB

conf.py(仅路径):

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

类A.py:

class A:
    def sayNo(self):
        print('NO!')

B类.py:

from ClassA import A

class B:
    def sayNoB(self):
        no = A()
        no.sayNo()

来自命令行的警告:

WARNING: autodoc: failed to import module 'ClassB' from module 'app'; the following exception was raised:
No module named 'ClassA'

标签: pythonpython-3.xpython-sphinxautodoc

解决方案


至于我,你应该使用相对导入 - 与dot

from .ClassA import A

推荐阅读