sphinx autoclass不导入模块

时间:2017-08-29 20:55:31

标签: python python-sphinx

冒着被告知我没有充分研究这个问题的风险(我现在已经参加了为期一周的好事),我无法在sphinx中使用autoclass功能。我收到一系列导入错误。我已将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 doc(对于文档包来说很讽刺)真的很难用于超出微不足道并且也不是非常复杂的例子。

对于问题所在的任何帮助都将非常感激。

1 个答案:

答案 0 :(得分:0)

首先,始终粘贴错误堆栈,以减少那些愿意回答的人的工作:

$ 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无法导入课程&#39;笑话&#39;&#34;,并继续......

      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。它用各种红色和黄色标记标记了你的代码。

text2.py in PyCharm

额外提示#2:您__init__.py中不需要任何导入声明。它可以是空白的。

相关问题