Skip to main content
 首页 » 编程设计

python之"fails to import module"在 Ubuntu 18.04 上运行 sphinxcontrib-matlabdomain 的 test_data 示例时

2024年11月24日8www_RR

我正在尝试运行在 sphinxcontrib-matlabdomain pypi site 上给出的示例/演示但是 autodoc 模块无法导入 matlab 示例文件。

我使用 sphinx-quickstart 设置了 sphinx 环境。操作系统是 Ubuntu 18.04。我的目录结构是这样的

root 
|--------test_data 
|                |--MyHandleClass.m 
| 
| 
|--------conf.py 
|--------Makefile 
|--------index.rst 

我的config.py的相关部分是

import os 
: 
: 
extensions = [ 
    'sphinxcontrib.matlab', 
    'sphinx.ext.autodoc', 
] 
: 
: 
matlab_src_dir = os.path.abspath('.') 
print "matlab_src_dir: ", matlab_src_dir 

第一个文件是从例子中复制过来的

Test Data 
========= 
This is the test data module. 
 
.. automodule:: test_data 
 
:mod:`test_data` is a really cool module. 
 
My Handle Class 
--------------- 
This is the handle class definition. 
 
.. autoclass:: MyHandleClass 
 
    :show-inheritance: 
    :members:  

运行make html时的输出是

Running Sphinx v1.8.5 
matlab_src_dir:  /home/xxxxx/Desktop/sphinxtest 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'automodule' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'autoclass' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'autoexception' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'autodata' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'autofunction' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'automethod' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'autoattribute' is already registered, it will be overridden 
WARNING: while setting up extension sphinx.ext.autodoc: directive 'autoinstanceattribute' is already registered, it will be overridden 
loading pickled environment... done 
building [mo]: targets for 0 po files that are out of date 
building [html]: targets for 0 source files that are out of date 
updating environment: [] 0 added, 1 changed, 0 removed 
reading sources... [100%] test_data                                                                                                     
WARNING: autodoc: failed to import module u'test_data'; the following exception was raised: 
No module named test_data 
WARNING: don't know which module to import for autodocumenting u'MyHandleClass' (try placing a "module" or "currentmodule" directive in the document, or giving an explicit module name) 
looking for now-outdated files... none found 
pickling environment... done 
checking consistency... done 
preparing documents... done 
writing output... [100%] test_data                                                                                                      
generating indices... genindex 
writing additional pages... search 
copying static files... done 
copying extra files... done 
dumping search index in English (code: en) ... done 
dumping object inventory... done 
build succeeded, 10 warnings. 
 
The HTML pages are in _build/html. 

我愿意相信我是按照说明操作的。但是我在这里遗漏了什么吗?

请您参考如下方法:

扩展的顺序很重要,文档中可能没有完全清楚这一点。所以 conf.py 文件应该看起来像这样。

import os 
: 
: 
extensions = [ 
    'sphinx.ext.autodoc', 
    'sphinxcontrib.matlab', 
] 
: 
: 
matlab_src_dir = os.path.abspath('.') 
print "matlab_src_dir: ", matlab_src_dir 

注意(2019-11-09):在 0.10.0 版本中,这不再是问题,因为 Python 和 MATLAB 自动文档可以共存。