sphinx autoclass未导入modu

2024-04-20 16:08:55 发布

您现在位置:Python中文网/ 问答频道 /正文

冒着被告知我没有对此进行足够的研究的风险(我已经在这方面做了一个星期了),我无法让autoclass特性在sphinx中工作。我得到一系列导入错误。我已经将sys.path.insert(0,os.path.abspath('.'))sys.path.insert(0, os.path.abspath('..'))都添加到了conf.py文件中,所以这不应该是原因,因为我也尝试了一大堆其他文件。在

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

但jist是这样的:

在该结构的回购中:

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

其中text.pytext2.py包含如下简单类:

^{pr2}$

text.rst是:

Sad
===

Sad story

.. autoclass:: text.Sad
   :members:

我不明白为什么它不起作用。显然,我遗漏了一些东西,但我发现sphinx doc(对于文档包来说是讽刺的)对于那些超出琐碎、也不是超级复杂的例子来说确实很难理解。在

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


Tags: 文件pathtextpyossphinxsysrst
1条回答
网友
1楼 · 发布于 2024-04-20 16:08:55

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

$ 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.

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

^{pr2}$

…表示在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中,Python解释器希望在第10行的方法定义之后有更多的缩进。所以缩进方法的return语句。在

一旦你纠正了这两个错误,你就应该表现得很好了。在

额外提示1:使用代码编辑器检查Python语法是否存在简单错误。我喜欢PyCharm。它用各种红黄旗标记你的代码。在

text2.py in PyCharm

额外提示2:您的__init__.py中不需要任何import语句。它可以是空白的。在

相关问题 更多 >