问题描述
我正在尝试生成文档,但在命令行中遇到警告。我一直在搜索该网站,但找不到适合我的解决方案。我将原因缩小到 ClassB 中的导入。
我的文件夹结构是:
classA.rst:
classA
======
.. automodule:: app.ClassA
:members:
classB.rst:
classB
======
.. automodule:: app.ClassB
:members:
index.rst(仅 toctree):
.. toctree::
:maxdepth: 2
:caption: Contents:
classA
classB
conf.py(仅路径):
sys.path.insert(0,os.path.abspath('../src'))
ClassA.py:
class A:
def sayNo(self):
print('NO!')
ClassB.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'
解决方法
至于我,你应该使用相对导入 - dot
from .ClassA import A