python - 如何为 Sphinx 正确设置项目目录
问题描述
我查看了许多教程和视频,其中介绍了使用 Sphinx 生成文档的过程,此时我确信我的项目结构有问题。我正在运行 Windows 10 Pro,2004;Python 3.7.9;狮身人面像 3.3.0。
sphinx_test
我按照以下步骤创建了一个全新的项目来测试我的 Sphinx 技能:
- 创建一个新的虚拟环境
python -m virtualenv sphinxtest
- 激活它
sphinxtest\Scripts\activate
- 安装 Sphinx 和要求
pip install sphinx
- 创建一个新的项目文件夹
mkdir sphinx_test
并导航到它cd sphinx_test
- 为源代码和所有子模块创建子目录
mkdir src
*.py
使用随机文档字符串(nopes.py
和sqrer.py
)创建一些愚蠢的示例文件- 创建 docs 目录
mkdir docs
并导航到它cd docs
- 执行
sphinx-quickstart --ext-autodoc
,选择单独build
的source
目录 - 修改
conf.py
,添加./src/
到PATH
- 修改
index.rst
,在组modules
下面添加toctree
:
sphinx-apidoc -o .\source\ ..\src\
从目录内部运行docs
。- 运行
make html
并获得以下信息:
Running Sphinx v3.3.0
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 2 source files that are out of date
updating environment: 0 added, 2 changed, 0 removed
reading sources... [100%] src
WARNING: autodoc: failed to import module 'nopes' from module 'src'; the following exception was raised:
No module named 'src'
WARNING: autodoc: failed to import module 'sqrer' from module 'src'; the following exception was raised:
No module named 'src'
WARNING: autodoc: failed to import module 'src'; the following exception was raised:
No module named 'src'
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] src
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, 3 warnings.
The HTML pages are in build\html.
这是目前的文件夹结构:
C:.
├───docs
│ │ make.bat
│ │ Makefile
│ │
│ ├───build
│ │ ├───doctrees
│ │ │ environment.pickle
│ │ │ index.doctree
│ │ │ modules.doctree
│ │ │ src.doctree
│ │ │
│ │ └───html
│ │ │ .buildinfo
│ │ │ genindex.html
│ │ │ index.html
│ │ │ modules.html
│ │ │ objects.inv
│ │ │ search.html
│ │ │ searchindex.js
│ │ │ src.html
│ │ │
│ │ ├───_sources
│ │ │ index.rst.txt
│ │ │ modules.rst.txt
│ │ │ src.rst.txt
│ │ │
│ │ └───_static
│ │ alabaster.css
│ │ basic.css
│ │ custom.css
│ │ doctools.js
│ │ documentation_options.js
│ │ file.png
│ │ jquery-3.5.1.js
│ │ jquery.js
│ │ language_data.js
│ │ minus.png
│ │ plus.png
│ │ pygments.css
│ │ searchtools.js
│ │ underscore-1.3.1.js
│ │ underscore.js
│ │
│ └───source
│ │ conf.py
│ │ index.rst
│ │ modules.rst
│ │ src.rst
│ │
│ ├───_static
│ └───_templates
└───src
nopes.py
sqrer.py
__init__.py
这是我的conf.py
:
# Configuration file for the Sphinx documentation builder.
#
# This file only contains a selection of the most common options. For a full
# list see the documentation:
# https://www.sphinx-doc.org/en/master/usage/configuration.html
# -- Path setup --------------------------------------------------------------
# If extensions (or modules to document with autodoc) are in another directory,
# add these directories to sys.path here. If the directory is relative to the
# documentation root, use os.path.abspath to make it absolute, like shown here.
import os
import sys
import pathlib
path = pathlib.Path(__file__).resolve() / '..' / '..' / '..' / 'src'
# sys.path.insert(0, os.path.abspath('..\src'))
sys.path.insert(0, os.path.abspath(path))
# -- Project information -----------------------------------------------------
project = 'TEST PROJECT'
copyright = '2020, Cerberton'
author = 'Cerberton'
# -- General configuration ---------------------------------------------------
# Add any Sphinx extension module names here, as strings. They can be
# extensions coming with Sphinx (named 'sphinx.ext.*') or your custom
# ones.
extensions = [
'sphinx.ext.autodoc',
]
# Add any paths that contain templates here, relative to this directory.
templates_path = ['_templates']
# List of patterns, relative to source directory, that match files and
# directories to ignore when looking for source files.
# This pattern also affects html_static_path and html_extra_path.
exclude_patterns = []
# -- Options for HTML output -------------------------------------------------
# The theme to use for HTML and HTML Help pages. See the documentation for
# a list of builtin themes.
#
html_theme = 'alabaster'
# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
# so a file named "default.css" will overwrite the builtin "default.css".
html_static_path = ['_static']
# -- Extension configuration -------------------------------------------------
解决方案
推荐阅读
- angular - Angular 10 Web Worker:找不到基本 TypeScript 配置文件“tsconfig.base.json”
- c# - 循环遍历 C# 列表
- r - Webscrape - R - 从复杂页面中提取
- node.js - 将 MERN 应用程序部署到 Azure,一直使用“localhost”在本地运行
- ruby-on-rails - 在 macOS 11.0 上安装 gem 时 Ruby 2.7.1 系统中断
- android - 更改 BottomNavigationView 中的图标颜色
- python-3.x - plot_importance 中的特征名称
- java - 使用 Thymeleaf 解析片段模板 Spring Boot 时出错
- c - malloc 是否保证只分配分配的内存块?
- matrix - Matlab如何存储三列数据