【问题标题】:Sphinx can't find the root module of my project [duplicate]Sphinx 找不到我的项目的根模块 [重复]
【发布时间】: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


【解决方案1】:

在您的 conf.py 中,输入sys.path.insert(0, "..")../ 相对路径导航到父目录。由于 conf.py 位于 docs/ 中,因此 docs/ 的父目录是您项目的根目录。这样,sphinx 将能够访问您的项目文件并能够导入您的模块。

【讨论】:

    猜你喜欢
    • 2020-10-02
    • 2020-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 2014-07-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多