【发布时间】:2021-07-11 17:09:44
【问题描述】:
我有这个示例项目来了解 Sphinx 的工作原理。
目录结构如下:
.
+--app
| +--api
| +--endpoints
| +--address.py
|
|--docs
| +--conf.py
| +--index.rst
| +--source
| +--modules.rst
| +--address.rst
我已经在conf.py的路径中添加了相关的文件路径,如下:
import os
import sys
from pathlib import Path
project_root = Path(os.getcwd()).parent.absolute()
sys.path.insert(0, project_root)
path_app = os.path.join(project_root, "app")
sys.path.insert(0, path_app)
path_address = os.path.join(project_root, "app", "api", "endpoints")
sys.path.insert(0, path_address)
我有我的 ./docs/index.rst 文件,由 Sphinx 自动生成:
.. Sky-Payment documentation master file, created by
sphinx-quickstart on Fri Apr 16 17:23:31 2021.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Sky-Payment's documentation!
=======================================
.. toctree::
:maxdepth: 2
:caption: Contents:
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
然后我有我的docs/source/modules.rst 文件:
app
===
.. toctree::
:maxdepth: 4
address
还有我的docs/source/address.rst 文件:
address module
==============
.. automodule:: app.api.endpoints.address
:members:
:undoc-members:
:show-inheritance:
当我从 .\docs 运行 sphinx-build.exe -b html . .\_build 时,我收到此错误:
WARNING: autodoc: failed to import module 'api.endpoints.address' from
module 'app'; the following exception was raised: No module named
'app'
我在这里做错了什么?
【问题讨论】:
-
在你的 conf.py 中,输入
sys.path.insert(0, "..") -
您没有在包中包含
__init__.py文件,因此无法回答这个问题。您还忘记在index.rst的目录树中包含modules。
标签: python windows-10 python-sphinx