2014-08-28 61 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