2017-08-29 31 views
0

冒着被告知我沒有足夠的研究(我一直在這裏一個星期的好處),我不能讓autoclass功能在獅身人面像工作。我收到一系列導入錯誤。我已經將sys.path.insert(0,os.path.abspath('.'))sys.path.insert(0, os.path.abspath('..'))添加到conf.py文件,所以不應該是原因,因爲我也嘗試了大量其他文件。獅身人面像autoclass不導入模塊

我做了一個小例子回購在這裏:GitHub

但JIST是這樣的:

在結構的回購:

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: 

我不明白爲什麼它不起作用。很明顯,我錯過了一些東西,但我發現獅身人面像文檔(對於文檔包來說是諷刺的)確實很難遵循那些超越平凡並且不是非常複雜的例子。

任何有關問題出在哪裏的幫助將不勝感激。

回答

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不能「進口類‘惡搞’」,並繼續...

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

    ^
IndentationError: expected an indented block 

...這意味着,在text2.py,Python解釋器在線路方法定義後,預計更多的壓痕10.縮小該方法的return聲明。

一旦你解決了這兩個錯誤,你應該很好。

Bonus tip#1:使用代碼編輯器檢查您的Python語法是否存在簡單錯誤。我喜歡PyCharm。它用各種紅色和黃色標誌標記你的代碼。

text2.py in PyCharm

特別提示#2:你並不需要在__init__.py任何import語句。它可以是空白的。