【问题标题】:Sphinx doesn't find Python packages when using autodoc使用 autodoc 时,Sphinx 找不到 Python 包
【发布时间】:2013-07-28 11:10:24
【问题描述】:

我正在尝试为测试 Python 项目创建文档,然后再为实际项目创建文档。 我的系统:Win7 64位,python 2.7.5 64位。

我的项目名称是 testDoc。它包括python包,命名为t,其中包括2个模块t1t2__init__.py

__init__.py 包含:

import t1
import t2

t1.py 包含:

'''
Created on 27  2013

@author: 
'''

class MyClass(object):
    '''
    Hi
    '''


    def __init__(self,selfparams):
        '''
        Constructor
        '''
        pass

为了创建文档,我在 testDoc 的命令行中运行:

sphinx-apidoc -A "me" -F -o docs .

Sphinx 创建了许多文件,根据 Sphinx 文档可以。 然后,conf.py被修改

sys.path.insert(0, os.path.abspath(absolute path to testDoc))

我进入docs文件夹并输入

make html

并得到以下错误输出:

Making output directory...
Running Sphinx v1.1.3
loading pickled environment... not yet created
building [html]: targets for 2 source files that are out of date
updating environment: 2 added, 0 changed, 0 removed
reading sources... [ 50%] index
reading sources... [100%] t
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\sphinx\ext\autodoc.py", line 321, in impor
t_object
    __import__(self.modname)
ImportError: No module named t.__init__
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\sphinx\ext\autodoc.py", line 321, in impor
t_object
    __import__(self.modname)
ImportError: No module named t.t1
Traceback (most recent call last):
  File "C:\Python27\lib\site-packages\sphinx\ext\autodoc.py", line 321, in impor
t_object
    __import__(self.modname)
ImportError: No module named t.t2

...testDoc\t\docs\t.rst:7: WARNING: a
autodoc can't import/find module 't.__init__', it reported error: "No module name
d t.__init__", please check your spelling and sys.path
...testDoc\t\docs\t.rst:15: WARNING:
autodoc can't import/find module 't.t1', it reported error: "No module named t.t
1", please check your spelling and sys.path
t...testDoc\t\docs\t.rst:23: WARNING:
autodoc can't import/find module 't.t2', it reported error: "No module named t.t
2", please check your spelling and sys.path
looking for now-outdated files... none found
pickling environment... done
checking consistency... done
preparing documents... done
writing output... [ 50%] index
writing output... [100%] t

writing additional files... (0 module code pages) genindex search
copying static files... done
dumping search index... done
dumping object inventory... done
build succeeded, 3 warnings.

Build finished. The HTML pages are in _build/html.

怎么了? 谢谢。

【问题讨论】:

    标签: python python-sphinx sphinx-apidoc


    【解决方案1】:

    我不知道“testDoc 的绝对路径”是什么,但从 Sphinx 输出中我可以看到 testDoc 目录结构是 testDoc/t/docsdocs 目录是conf.py 所在的位置。

    要正确设置模块搜索路径,您需要从conf.py 上两级:

    sys.path.insert(0, os.path.abspath("../.."))
    

    【讨论】:

      猜你喜欢
      • 2020-01-05
      • 2016-07-24
      • 1970-01-01
      • 2020-11-01
      • 2011-04-10
      • 1970-01-01
      • 1970-01-01
      • 2011-02-11
      • 2012-02-09
      相关资源
      最近更新 更多