繁体   English   中英

sphinx autoclass不导入模块

[英]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:

我不明白为什么它不起作用。 显然,我缺少了一些东西,但是我发现狮身人面像doc(文档包的讽刺意味)真的很难遵循,这些例子超出了琐碎的范围,而且也不是超级复杂。

对于问题所在的任何帮助将不胜感激。

首先,始终粘贴错误堆栈以减少将要回答的人员的工作量,例如:

$ 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无法“导入类'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。 它用各种红色和黄色标志标记了您的代码。

PyCharm中的text2.py

温馨提示#2: __init__.py不需要任何导入语句。 可以为空白。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM