我正在尝试学习如何使用Sphinx来记录Python代码,并且在识别我的模块和类来记录文档字符串时遇到了问题。我有一个简单的python类写入下面显示的Statistics.py
文件,该文件存储在目录/Users/MyName/desktop/Code_Test/Test
中。
import sys
sys.path.insert(0,"/Users/MyName/desktop/Code_Test.d/Test")
"""
Author: Jon Webb
Date: March 27, 2017
"""
class Test
""" This documents my class"""
def __init(self,Data):
def Sum(self):
""" This documents a member function"""
return (np.sum(self.Data))
我正在尝试使用它作为测试用例来学习如何使用Sphinx记录Python代码。我在代码所在的目录中运行sphinx-quickstart
,并在快速入门设置期间将其声明为根目录。正如预期的那样,它会生成许多文件,包括_build
,_static
,_template
,conf.py
和index.rst
。我已按以下格式修改了index.rst
文件,以使其识别出Test.py
模块存在且包含Test
类。
.. Test documentation master file, created by
sphinx-quickstart on Mon Mar 27 14:28:47 2017.
You can adapt this file completely to your liking, but it should at least
contain the root `toctree` directive.
Welcome to Test's documentation!
================================
.. toctree::
:maxdepth: 2
:caption: Contents:
.. automodule:: Statistics
.. autoclass:: Test
:members:
Indices and tables
==================
* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
但是,当我运行make html
时,我收到以下警告WARNING: autofocus failed to import module 'Statistics'; the following exception was raised: Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages/sphinx/ext/autodoc.py", line 551, in import_object __import__(self.modname) File "/Users/jonwebb/desktop/Code_Test.d/Test/Statistics.py", line 7
class Test
。换句话说,它似乎无法识别标题为Statistics.py
的文件存在,因此无法找到Test
类。我缺少什么以及如何让代码引入文档字符串并正确记录类和成员函数?