简体   繁体   English

从文档字符串生成狮身人面像文档

[英]Generate sphinx docu from docstrings not working

I have a project with the following structure (which I would like to keep): 我有一个具有以下结构的项目(我希望保留):

my_project
├── build  # here is where sphinx should dump into
├── requirements.txt
├── make.bat
├── Makefile
├── ...  # more config files
├── doc  # this is where I want sphinx files to live
│   ├── conf.py
│   └── index.rst
├── src
│   └── my_project
│       ├── __init__.py
│       ├── module_1
│       │   ├── __init__.py
│       │   └── ...
│       └── util
│           ├── __init__.py
│           └── ...
└── tests
    ├── module_1
    │   ├── __init__.py
    │   └── ...  # testing module 1
    └── util
        ├── __init__.py
        └── ...  # testing util stuff

I recreated it on github , which can be used to recreate the results by executing my_setup.sh within. 在github上重新创建了它,可以通过其中执行my_setup.sh来重新创建结果。

I want to build the documentation from docstrings. 我想从文档字符串构建文档。 I used sphinx's quickstart to generate necessary config, but when I call make hmtl , the resulting docu doesn't include any docstrings from my source code, ie everything in my_project/src/my_project . 我使用了sphinx的快速入门来生成必要的配置,但是当我调用make hmtl ,生成的文档不包含源代码中的任何文档字符串,即my_project/src/my_project所有文档字符串。 Sphinx's documenation is a bit overwhelming, given that I feel that I am trying to set up something very basic. Sphinx的文档处理有点让人不知所措,因为我觉得我正在尝试建立一些非常基本的东西。

Relevant info from config files (please tell me if I forgot something important): 配置文件中的相关信息(请告诉我是否忘记了重要信息):

Makefile Makefile文件

SPHINXOPTS    =
SPHINXBUILD   = sphinx-build
SPHINXPROJ    = my_project
SOURCEDIR     = doc
BUILDDIR      = build
...

make.bat make.bat

set SOURCEDIR=doc
set BUILDDIR=build
set SPHINXPROJ=my_project
...

conf.py conf.py

import os
import sys
sys.path.insert(0, os.path.abspath('../src/my_project'))
...
extensions = [
    'sphinx.ext.autodoc',
    'sphinx.ext.todo',
    'sphinx.ext.coverage',
]
...

I tried this as well, but it first put a bunch of build files into doc that I'd rather not have there and it also didn't find any of the modules (fixed by omitting the -F parameter): 我也尝试过方法,但是它首先将一堆我不希望在其中的构建文件放到doc ,并且它也找不到任何模块(通过省略-F参数来修复):

$ sphinx-apidoc -F -o doc/ src/my_project/
$ cd doc
$ make html
Running Sphinx v1.7.2
loading pickled environment... done
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 0 source files that are out of date
updating environment: 0 added, 2 changed, 0 removed
reading sources... [100%] my_project.util                                                                                                                                                                                  
WARNING: autodoc: failed to import module 'my_project'; the following exception was raised:
No module named 'my_project'
WARNING: autodoc: failed to import module 'my_project.util.test_file'; the following exception was raised:
No module named 'my_project'
WARNING: autodoc: failed to import module 'my_project.util'; the following exception was raised:
No module named 'my_project'
looking for now-outdated files... none found
pickling environment... done
checking consistency... /home/arne/workspace/git/my_project/doc/my_project.rst: WARNING: document isn\'t included in any toctree
done
preparing documents... done
writing output... [100%] my_project.util                                                                                                                                                                                   
generating indices... genindex
writing additional pages... search
copying static files... done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 4 warnings.

There are a couple of issues with your MCVE. MCVE存在几个问题。

  1. rST source files must not reside in the output directory build , and should be in the docs source directory docs . rST源文件不得驻留在输出目录内部build ,而应位于docs源目录docs You should have done this instead: sphinx-apidoc -o docs src/my_project . 您应该改为这样做: sphinx-apidoc -o docs src/my_project
  2. As @mzjn mentioned, you need to uncomment and add some lines to your conf.py to resolve the WARNING: autodoc: failed to import module errors. 作为@mzjn提到的,你需要取消注释并添加一些行到conf.py来解决WARNING: autodoc: failed to import module错误。

     # -- 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 # sys.path.insert(0, os.path.abspath('.')) sys.path.insert(0, os.path.abspath('../src/')) 

After those two changes, I was able to successfully build your docs with its API. 经过这两个更改,我能够使用其API成功构建您的文档。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM