简体   繁体   English

Sphinx autodoc 无法导入模块

[英]Sphinx autodoc fails to import module

I'm trying to document a project using Sphinx, and am running into an issue where only some modules are being imported from a folder.我正在尝试使用 Sphinx 记录一个项目,但遇到了一个问题,即仅从文件夹中导入了一些模块。 My project structure looks like this:我的项目结构如下所示:

Project
|
|--Main
|    |--Scripts
|          __init__.py
|          libsmop.py
|          conv_table.py
|          f_discrim.py
|          recipes.py
|          ...

When I try to run make html , libsmop and recipes are imported without any issue, however conv_table and f_discrim get the following error:当我尝试运行make html ,导入libsmoprecipes没有任何问题,但是conv_tablef_discrim收到以下错误:

WARNING: autodoc: failed to import module u'conv_table' from module u'Scripts'; the following exception was raised:No module named conv_table

I don't think it's my config file because it's finding all of the files when I run sphinx-apidoc -o _rst Main/Scripts and I've confirmed that they appear in the resulting Scripts.rst file.我不认为这是我的配置文件,因为当我运行sphinx-apidoc -o _rst Main/Scripts时它会找到所有文件,并且我已经确认它们出现在生成的Scripts.rst文件中。

Why is autodoc finding some modules but not others?为什么 autodoc 会找到一些模块而不是其他模块?

Edit: conv_table.py is of this form:编辑: conv_table.py是这种形式:

import re
import numpy as np

"""
conv_table dictionary at the bottom of this file maps from matlab functions
to their python equivalents.
"""

def get_args(line,separator=",", open_char='(', close_char=')'):
    """Returns the arguments of line

    >>> get_args('ones(3,1,length(arr))')

...
< a bunch of function definitions>
...

conv_table = {... < a very big dictionary > ...}

Since your autodoc is picking up some of the modules, it may be because the dependencies of the failed modules are either 1) not imported correctly or 2) not installed under your python environment.由于您的 autodoc 正在获取一些模块,这可能是因为失败模块的依赖项 1) 未正确导入或 2) 未安装在您的 python 环境下。 You will want to check if all the import statements work within your failed modules.您将需要检查所有导入语句是否在您失败的模块中工作。

You will want to check the module loading path, according to the Sphinx docs :根据Sphinx 文档,您需要检查模块加载路径:

For Sphinx (actually, the Python interpreter that executes Sphinx) to find your module, it must be importable.为了让 Sphinx(实际上是执行 Sphinx 的 Python 解释器)找到您的模块,它必须是可导入的。 That means that the module or the package must be in one of the directories on sys.path – adapt your sys.path in the configuration file accordingly.这意味着模块或包必须位于 sys.path 上的目录之一 - 相应地调整配置文件中的 sys.path。

Also it would be useful to know how your __init__.py in Scripts directory looks like and how the conv_table module looks like as well.了解__init__.py在 Scripts 目录中的样子以及conv_table模块的样子也很有用。

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

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