警告:autodoc:尝试使用 sphinx 生成 python 文档时导入模块失败

问题描述 投票:0回答:0

我已经查看了 sphinx 文档以生成 python 文档。当我使用不引用任何 python 源代码的 .rst 文件生成 html 格式的文档时,文档生成正确。

在我的配置中,我使用以下文件:conf.py、index.rst、tutorial.rst、project.rst 和 pkg_app.rst

附上各自的内容:

conf.py:

import os
import sys
sys.path.insert(0, os.path.abspath('.'))

# -- General configuration ------------------------------------------------


# Configuration file for the Sphinx documentation builder.
#
# For the full list of built-in configuration values, see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html

# -- Project information -----------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#project-information

project = 'hmed'
copyright = '2023, Ramiro Jativa'
author = 'Ramiro Jativa'
release = '1.0.41'

# -- General configuration ---------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#general-configuration

extensions = ['sphinx.ext.autodoc']

templates_path = ['_templates']
exclude_patterns = ['_build', 'Thumbs.db', '.DS_Store']



# -- Options for HTML output -------------------------------------------------
# https://www.sphinx-doc.org/en/master/usage/configuration.html#options-for-html-output

html_theme = 'alabaster'
html_static_path = ['_static']

index.rst:

.. hmed documentation master file, created by
   sphinx-quickstart on Thu Apr 27 17:53:25 2023.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to hmed's documentation!
================================

Requirements for production:

MySQL
Python
Flask
PyPi Libraries

Requirements for development:

sqlite3
Python
Flask
PyPi Libraries 

Project structure:

project_name/pkg_app (include the source code distributed in various modules. And includes the __init__.py file)
project_name/sphinx_documentation (is the folder where the sphinx documentation is generated)

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

   tutorial
   project
   pkg_app

Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`

tutorial.rst:

Tutorial
========

Modules
-------

Module 1: Tutorial for module 1.

This is the content for tutorial 1.

Module 2: Tutorial for module 2.

This is the content for tutorial 2.

project.rst:

Project Documentation
=====================

Achieved goals
--------------

Goal 1: This is the description of goal 1.

This is the detail of what was done to achieve goal 1.

Goal 2: This is the description of goal 2.

This is the detail of what was done to achieve goal 2.

Learned lessons
---------------

This paragraph describes what was learned in the project.

pkg_app.rst:

Source code documentation
=========================

The pkg_app folder includes the __init__.py, models.py, forms.py, and routes.py files

使用指示的配置,我继续执行 make html 命令,sphinx 正确生成了文档。

接下来我包括执行 make html 命令的日志:

(venv_hmed) $ make clean
Removing everything under '_build'...
(venv_hmed) $ make html
Running Sphinx v5.3.0
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 4 source files that are out of date
updating environment: [new config] 4 added, 0 changed, 0 removed
reading sources... [100%] tutorial                                                                                           
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] tutorial                                                                                            
generating indices... genindex done
writing additional pages... search done
copying static files... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded.

The HTML pages are in _build/html.

结果,文档以html格式获取,如下所示:

到目前为止一切正常。

为了生成 pkg_app 目录结构中扩展名为 .py 的文件的文档,我将 pkg_app.rst 文件的内容修改为以下内容:

pkg_app.rst:

Source code documentation
=========================

The pkg_app folder includes the __init__.py, models.py, forms.py, and routes.py files

Module contents
---------------

.. automodule:: pkg_app
   :members:
   :undoc-members:
   :show-inheritance:

在sphinx中输入make html命令生成文档时,出现如下警告信息:

(venv_hmed) $ make clean
Removing everything under '_build'...
(venv_hmed) $ make html
Running Sphinx v5.3.0
making output directory... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 4 source files that are out of date
updating environment: [new config] 4 added, 0 changed, 0 removed
reading sources... [100%] tutorial                                                                                           
WARNING: autodoc: failed to import module 'pkg_app'; the following exception was raised:
No module named 'pkg_app'
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] tutorial                                                                                            
generating indices... genindex done
writing additional pages... search done
copying static files... done
copying extra files... done
dumping search index in English (code: en)... done
dumping object inventory... done
build succeeded, 1 warning.

The HTML pages are in _build/html.

问题:

pkg_app.rst 文件的正确配置是什么,以便它可以生成 pkg_app 包内所有模块的 python 文档?

谢谢。

python python-3.x python-sphinx sphinx
© www.soinside.com 2019 - 2024. All rights reserved.