sphinx-build失败 - autodoc无法导入/查找模块

Ric*_*ico 82 python documentation python-sphinx

我正试图开始使用Sphinx并且似乎有无情的问题.

命令: docs/sphinx-quickstart

我回答所有问题,一切正常.

命令: docs/ls

一切看起来都很正常.结果:build Makefile source

命令: sphinx-build -d build/doctrees source build/html

它似乎工作.我能够打开index.html文件,看到我想要的"shell".

当我尝试将我的实际源代码作为source文件夹时遇到问题.

命令: sphinx-build -d build/doctrees ../ys_utils build/html

结果:

Making output directory...
Running Sphinx v1.1.3
loading pickled environment... not yet created
No builder selected, using default: html
loading intersphinx inventory from http://docs.python.org/objects.inv...
building [html]: targets for 1 source files that are out of date
updating environment: 1 added, 0 changed, 0 removed
Traceback (most recent call last):                                                                                               
  File "/usr/local/lib/python2.6/dist-packages/Sphinx-1.1.3-py2.6.egg/sphinx/ext/autodoc.py", line 321, in import_object
    __import__(self.modname)
ImportError: No module named ys_utils
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/Sphinx-1.1.3-py2.6.egg/sphinx/ext/autodoc.py", line 321, in import_object
    __import__(self.modname)
ImportError: No module named ys_utils.test_validate_ut
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/Sphinx-1.1.3-py2.6.egg/sphinx/ext/autodoc.py", line 321, in import_object
    __import__(self.modname)
ImportError: No module named ys_utils.git_utils
Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/Sphinx-1.1.3-py2.6.egg/sphinx/ext/autodoc.py", line 321, in import_object
    __import__(self.modname)
ImportError: No module named setup.setup

/home/ricomoss/workspace/nextgen/ys_utils/ys_utils.rst:4: WARNING: autodoc can't import/find module 'ys_utils', it reported error: "No module named ys_utils", please check your spelling and sys.path
/home/ricomoss/workspace/nextgen/ys_utils/ys_utils.rst:10: WARNING: autodoc can't import/find module 'ys_utils.test_validate_ut', it reported error: "No module named ys_utils.test_validate_ut", please check your spelling and sys.path
/home/ricomoss/workspace/nextgen/ys_utils/ys_utils.rst:12: WARNING: don't know which module to import for autodocumenting u'UnitTests' (try placing a "module" or "currentmodule" directive in the document, or giving an explicit module name)
/home/ricomoss/workspace/nextgen/ys_utils/ys_utils.rst:18: WARNING: autodoc can't import/find module 'ys_utils.git_utils', it reported error: "No module named ys_utils.git_utils", please check your spelling and sys.path
/home/ricomoss/workspace/nextgen/ys_utils/ys_utils.rst:24: WARNING: autodoc can't import/find module 'setup.setup', it reported error: "No module named setup.setup", please check your spelling and sys.path
WARNING: master file /home/ricomoss/workspace/nextgen/ys_utils/index.rst not found
looking for now-outdated files... none found
pickling environment... done
checking consistency... /home/ricomoss/workspace/nextgen/ys_utils/ys_utils.rst:: WARNING: document isn't included in any toctree
done
preparing documents... done
writing output... [ 50%] index                                                                                                   
Exception occurred:
  File "/usr/local/lib/python2.6/dist-packages/Sphinx-1.1.3-py2.6.egg/sphinx/environment.py", line 1213, in get_doctree
    f = open(doctree_filename, 'rb')
IOError: [Errno 2] No such file or directory: '/home/ricomoss/workspace/nextgen/docs/build/doctrees/index.doctree'
The full traceback has been saved in /tmp/sphinx-err-jjJ7gM.log, if you want to report the issue to the developers.
Please also report this if it was a user error, so that a better error message can be provided next time.
Either send bugs to the mailing list at <http://groups.google.com/group/sphinx-dev/>,
or report them in the tracker at <http://bitbucket.org/birkenfeld/sphinx/issues/>. Thanks!
Run Code Online (Sandbox Code Playgroud)

我是Sphinx的全新手,对这种文档来说相对较新.有人可以提供一些建议吗?

编辑:

我希望能够使用Makefile来处理这个问题.截至目前,我的项目中有两个文件夹.

nextgen/ls

docs ys_utils

我需要nextgen/docs/Makefile生成HTML ys_utils以及我将要拥有的所有其他模块.

bmu*_*bmu 78

Autodoc无法找到您的模块,因为它们不在sys.path.

你必须包括在路径到您的模块sys.path在你的conf.py.看看你的顶部conf.py(刚刚导入之后sys),有一个sys.path.insert()声明,你可以适应.

顺便说一句:您可以使用MakefileSphinx创建的文档来创建文档.打电话吧

make
Run Code Online (Sandbox Code Playgroud)

看看选项.

如果出现问题,请尝试:

make clean
Run Code Online (Sandbox Code Playgroud)

在跑步之前make html.

  • 在 `conf.py` 中,添加 `print(sys.executable)` 我的 sphinx-build 正在调用 py-3.8,但我的 venv 是 py-3.7。 (2认同)
  • 嗯,我通过 `pip` 安装了一个外部依赖项,在命令行中运行 Python 时我可以毫无问题地导入它。但 sphinx 不会拾取它并抛出错误“没有模块..”。`sys.executable` 指向正确的 Python 安装。可能是什么问题呢? (2认同)

rya*_*lon 35

这听起来像os.path.append()就是为乡亲工作正常,但如果你按照conf.py模板,需要插入模块路径的前sys.path使用os.path.insert(0, ...),并且只需添加一个额外的.

import os
import sys
sys.path.insert(0, os.path.abspath('..'))
Run Code Online (Sandbox Code Playgroud)

如果您已经安装你的sphinx项目中使用单独buildsource目录,该呼叫应改为:

sys.path.insert(0, os.path.abspath('../..'))
Run Code Online (Sandbox Code Playgroud)


Pra*_*a V 29

conf.py

只需添加项目文件夹的路径即可.

sys.path.append('/home/workspace/myproj/myproj')
Run Code Online (Sandbox Code Playgroud)

  • 如果你有一个像`/ app`,`/ docs`这样的项目结构,你可以使用`sys.path.append(os.path.join(os.path.dirname(__ name __),'..' ))`然后在`.rst`文件中使用`.. automodule :: app`. (17认同)
  • 路径硬编码不是你用`conf.py`做的最好的事情. (7认同)

Amo*_*gel 10

我不知道为什么(也许在我的情况下 autodoc 无法安装我的包),但我总是遇到module-not-found错误,直到我明确地将所有包含模块的目录包含到路径中。

对于以下示例文件夹结构

project_dir
|- setup.py
|- src
|  |- __init__.py
|  |- source1.py
|  |- sub_project
|     |- __init__.py
|     |- source2.py 
|- docs
    |- conf.py
    |- source
    |  |- index.rst
    |- _build       
Run Code Online (Sandbox Code Playgroud)

包括我

for x in os.walk('../../src'):
  sys.path.insert(0, x[0])
Run Code Online (Sandbox Code Playgroud)

到开头,conf.py以便添加所有涉及的目录。


Ing*_*ako 5

如果

  1. 模块根路径在 conf.py 中正确设置
  2. __init__.py 放置正确
  3. 第一个语法是正确的

并且您的 autodoc 仍然找不到模块...

可能是因为你的python环境下这些模块的依赖不满足。您将需要检查所有导入语句是否在模块中都有效。

  • 我不明白为什么 sphinx 需要依赖项,是因为可以在文档字符串中进行测试吗?这可以避免吗(我不需要任何包,我只想sphinx将docstring解析为html)。 (5认同)
  • 如果您不想导入这些依赖项,请在conf.py文件中使用autodoc_mock_imports:https://www.sphinx-doc.org/en/master/usage/extensions/autodoc.html#confval-autodoc_mock_imports (2认同)

Joh*_*Jr. 1

我想我第一次尝试将文件添加到目录树时就这样做了。我认为这是因为我遗漏了 :maxdepth 行和文件名之间的空行。

.. Animatrix Concepts documentation master file, created by
   sphinx-quickstart on Thu Mar 22 18:06:15 2012.
   You can adapt this file completely to your liking, but it should at least
   contain the root `toctree` directive.

Welcome to Animatrix Concepts documentation!
============================================

Contents:

.. toctree::
   :maxdepth: 2

   stuff


Indices and tables
==================

* :ref:`genindex`
* :ref:`modindex`
* :ref:`search`
Run Code Online (Sandbox Code Playgroud)

上面是我的index.rst 文件。stuff.rst 与它位于同一目录中。

  • 这会去哪里?我在“/docs/source”和“/ys_utils”中有“index.rst”。我猜这应该在“docs”版本中?我只是使用使用“sphinx-quickstart”创建的默认“index.rst”文件。 (2认同)