Sphinx 找不到我项目的根模块
Sphinx can't find the root module of my project
我有这个示例项目来了解 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, "..")
。 ../ 相对路径导航到父目录。因为conf.py位于docs/,所以docs/的父目录是你的项目的根。这样,sphinx
将能够访问您的项目文件并且能够导入您的模块。
我有这个示例项目来了解 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, "..")
。 ../ 相对路径导航到父目录。因为conf.py位于docs/,所以docs/的父目录是你的项目的根。这样,sphinx
将能够访问您的项目文件并且能够导入您的模块。