Sphinx AutoClass不导入模块

sphinx autoclass not importing module(Sphinx AutoClass不导入模块)

本文介绍了Sphinx AutoClass不导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

冒着被告知我没有充分研究这一点的风险(我已经在这方面研究了大半个星期),我无法在狮身人面像中使用自动类功能。我收到一系列导入错误。我已将sys.path.insert(0,os.path.abspath('.'))sys.path.insert(0, os.path.abspath('..'))添加到conf.py文件中,因此这不应该是原因,因为我还尝试了一系列其他文件。

我在这里做了一个小示例repo: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:

我不明白为什么它不起作用。显然,我遗漏了一些东西,但我发现狮身人面像的文档(对于文档包来说具有讽刺意味)真的很难效仿,因为它超出了琐碎的范围,而且也不是非常复杂。

如能就问题所在提供任何帮助,我们将不胜感激。

推荐答案

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

$ 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无法"导入类‘笑话’",并继续...

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

      ^
IndentationError: expected an indented block

...这意味着在text2.py中,在第10行的方法定义之后需要更多缩进。因此缩进该方法的return语句。

一旦您修复了这两个错误,您就应该很好了。

额外提示#1:使用代码编辑器来检查您的Python语法中的简单错误。我喜欢皮查姆。它用各种红色和黄色旗帜标记您的代码。

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

这篇关于Sphinx AutoClass不导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Sphinx AutoClass不导入模块

基础教程推荐