狮身人面像自动类不导入模块

sphinx autoclass not importing module

冒着被告知我对此研究不够的风险(我已经研究了一周的大部分时间),我无法在 sphinx 中使用自动分类功能。我收到一系列导入错误。我已经将 sys.path.insert(0,os.path.abspath('.')) sys.path.insert(0, os.path.abspath('..')) 添加到 conf.py 文件中,所以这不应该是原因,因为我也尝试了一大堆其他文件。

我在这里做了一个小示例回购:GitHub

但重点是:

在结构的回购中:

funniest
   funniest
      __init__.py
      text.py
      text2.py
   doc
      source
         index.rst
         text.rst
         text2.rst

其中 text.pytext2.py 包含简单的 类,如下所示:

class Sad(object):
    """Not so funny class
    """
    def sad_story(self):
        """This is a sob story
        """
        print('This is a very sad story')


    def sad_story2(self):
        """This is a sob story 2
        """
        print('This is a very very sad story')

text.rst是:

Sad
===

Sad story

.. autoclass:: text.Sad
   :members:

我不明白为什么它不起作用。显然,我遗漏了一些东西,但我发现 sphinx 文档(对于文档包来说具有讽刺意味)真的很难理解,因为这些示例不仅微不足道而且也不是超级复杂。

任何有关问题所在的帮助将不胜感激。

首先,始终粘贴错误堆栈以减少那些会回答的人的工作量,如下所示:

$ make html
sphinx-build -b html -d build/doctrees   source build/html
Running Sphinx v1.6.3
['/Users/stevepiercy/projects/funniest_example/funniest/doc', '/Users/stevepiercy/projects/funniest_example/funniest/doc/source', '/Users/stevepiercy/projects/funniest_example/funniest/env/bin', '/Users/stevepiercy/.pyenv/versions/3.6.1/lib/python36.zip', '/Users/stevepiercy/.pyenv/versions/3.6.1/lib/python3.6', '/Users/stevepiercy/.pyenv/versions/3.6.1/lib/python3.6/lib-dynload', '/Users/stevepiercy/projects/funniest_example/funniest/env/lib/python3.6/site-packages', '/Users/stevepiercy/projects/funniest_example/funniest']
loading pickled environment... failed: Can't get attribute 'WarningStream' on <module 'sphinx.util.nodes' from '/Users/stevepiercy/projects/funniest_example/funniest/env/lib/python3.6/site-packages/sphinx/util/nodes.py'>
building [mo]: targets for 0 po files that are out of date
building [html]: targets for 3 source files that are out of date
updating environment: 3 added, 0 changed, 0 removed
reading sources... [100%] text2
WARNING: /Users/stevepiercy/projects/funniest_example/funniest/doc/source/text.rst:6: (WARNING/2) autodoc: failed to import class 'Sad' from module 'text'; the following exception was raised:
Traceback (most recent call last):
  File "/Users/stevepiercy/projects/funniest_example/funniest/env/lib/python3.6/site-packages/sphinx/ext/autodoc.py", line 657, in import_object
    __import__(self.modname)
ModuleNotFoundError: No module named 'text'
WARNING: /Users/stevepiercy/projects/funniest_example/funniest/doc/source/text2.rst:6: (WARNING/2) autodoc: failed to import class 'Jokes' from module 'funniest.text2'; the following exception was raised:
Traceback (most recent call last):
  File "/Users/stevepiercy/projects/funniest_example/funniest/env/lib/python3.6/site-packages/sphinx/ext/autodoc.py", line 657, in import_object
    __import__(self.modname)
  File "/Users/stevepiercy/projects/funniest_example/funniest/funniest/__init__.py", line 2, in <module>
    from . import text2
  File "/Users/stevepiercy/projects/funniest_example/funniest/funniest/text2.py", line 10
    """Shitty joke
    """

      ^
IndentationError: expected an indented block
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [100%] text2
generating indices... genindex
writing additional pages... search
copying static files... WARNING: html_static_path entry '/Users/stevepiercy/projects/funniest_example/funniest/doc/source/.static' does not exist
done
copying extra files... done
dumping search index in English (code: en) ... done
dumping object inventory... done
build succeeded, 3 warnings.

Build finished. The HTML pages are in build/html.

警告准确说明了问题所在。第一个:

WARNING: /Users/stevepiercy/projects/funniest_example/funniest/doc/source/text.rst:6: (WARNING/2) autodoc: failed to import class 'Sad' from module 'text';

...表示在 text.rst 中您在第 6 行导入不正确。因此更改此:

.. autoclass:: text.Sad

对此:

.. autoclass:: funniest.text.Sad

第二次警告:

WARNING: /Users/stevepiercy/projects/funniest_example/funniest/doc/source/text2.rst:6: (WARNING/2) autodoc: failed to import class 'Jokes' from module 'funniest.text2'; the following exception was raised:

...说 text2.rst 不能 "import class 'Jokes'",然后继续...

      File "/Users/stevepiercy/projects/funniest_example/funniest/funniest/text2.py", line 10
    """Shitty joke
    """

      ^
IndentationError: expected an indented block

...这意味着在 text2.py 中,Python 解释器期望在第 10 行的方法定义之后有更多缩进。因此缩进方法的 return 语句。

一旦你修正了这两个错误,你就可以了。

奖励提示 #1:使用代码编辑器来检查您的 Python 语法是否存在简单错误。我喜欢PyCharm。它用各种红色和黄色标记标记了您的代码。

额外提示 #2:您的 __init__.py 中不需要任何导入语句。可以为空。