2014-08-28 62 views
6

我想在Python中记录一个包。目前,我有以下目录结构:如何使用Sphinx文档Python包

. 
└── project 
    ├── _build 
    │   ├── doctrees 
    │   └── html 
    │    ├── _sources 
    │    └── _static 
    ├── conf.py 
    ├── index.rst 
    ├── __init__.py 
    ├── make.bat 
    ├── Makefile 
    ├── mod1 
    │   ├── foo.py 
    │   └── __init__.py 
    ├── mod2 
    │   ├── bar.py 
    │   └── __init__.py 
    ├── _static 
    └── _templates 

这棵树是sphinx-quickstart发射的结果。在conf.py我没有注释sys.path.insert(0, os.path.abspath('.'))和我有extensions = ['sphinx.ext.autodoc']

index.rst是:

.. FooBar documentation master file, created by 
    sphinx-quickstart on Thu Aug 28 14:22:57 2014. 
    You can adapt this file completely to your liking, but it should at least 
    contain the root `toctree` directive. 

Welcome to FooBar's documentation! 
================================== 

Contents: 

.. toctree:: 
    :maxdepth: 2 

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

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

在所有__init__.py的我有一个文档字符串和同去的模块foo.pybar.py。但是,在项目中运行make html时,我没有看到任何docstings。

+0

你有一个单一的顶级index.rst文件,但没有别的。这还不够。您需要运行[sphinx-apidoc](http://sphinx-doc.org/man/sphinx-apidoc.html)来生成所需的.rst资源(或者“手工创建”它们)。 – mzjn 2014-08-28 13:10:40

+0

@mzjn你能详细点吗?我应该在哪个目录中调用'sphinx-apidoc'? 'sphinx'不会自动查看源代码? – Dror 2014-08-28 13:20:32

+0

为了生成API文档,Sphinx需要带有像automodule或autoclass这样的指令的.rst文件。它没有从源头中提取。也许你期望Sphinx像Epydoc或Doxygen一样工作,但事实并非如此。另请参阅以下答案:http://stackoverflow.com/a/2441159/407651,http://stackoverflow.com/a/6109098/407651。 – mzjn 2014-08-28 13:28:12

回答

3

这里是一个概述:使用文档字符串在源

  1. 文档你的包。
  2. 使用sphinx-quickstart创建一个Sphinx项目。
  3. 运行sphinx-apidoc生成设置为与autodoc一起使用的第一源。 将此命令与-F标志一起使用也会创建一个完整的Sphinx项目。如果你的API改变很多,你可能需要多次重新运行这个命令。
  4. 使用sphinx-build构建文档。

注:

+1

只是为了说清楚:例如'setup.py'需要保护。 – Dror 2014-08-29 07:31:52

+0

我应该在哪里运行'sphinx-apidoc'?生成的'.rst'文件有什么用处?把它们放在哪里?如何使用'-F'与'spinx-quickstart'不同? – minerals 2015-12-31 08:29:50

+0

@矿物。 *我应该在哪里运行sphinx-apidoc?*你的意思是“哪里”?问题是什么? *如何处理生成的.rst文件?放在哪里?*你把它们放在任何方便的地方。然后你运行'sphinx-build'就可以了。 – mzjn 2015-12-31 09:26:01