1

我可以使用 Sphinx 在本地自动生成我的项目的 API 文档,但是当我尝试使用 ReadTheDocs 时遇到了麻烦。当 ReadTheDocs 构建文档时,API 页面都是空白的(例如,参见这个)。读完这篇博文后,我意识到解决方案是模拟 numpy 和 matplotlib 之类的东西。所以我将以下行添加到我的conf.py

autodoc_mock_imports = ['numpy', 'matplotlib', 'matplotlib.pyplot', 'matplotlib.patches', 'matplotlib.path', 'matplotlib.lines', 'matplotlib.text', 'matplotlib.transforms', 'matplotlib.artist', 'cpickle']

添加后,由于以下两个 Sphinx 问题,我无法在本地或 ReadTheDocs 上构建 API 文档。

问题 #1:

当我构建文档时,出现以下错误。

/Users/Ben/Documents/Python/clearplot/doc/source/api/clearplot.rst:19: WARNING: autodoc: failed to import module u'clearplot'; the following exception was raised:
Traceback (most recent call last):
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sphinx/ext/autodoc.py", line 385, in import_object
__import__(self.modname)
File "/Users/Ben/Documents/Python/clearplot/clearplot/__init__.py", line 33, in <module>
mpl_version = _parse_version_string(_mpl.__version__)
File "/Users/Ben/Documents/Python/clearplot/clearplot/__init__.py", line 19, in _parse_version_string
v_list = v.split(".")
AttributeError: type object '__version__' has no attribute 'split'

我已经确认这个错误来自简单地做matplotlib.__version__.split("."). 我无法弄清楚如何快速解决此问题,因此我将其注释掉。

问题 #2:

现在,我在构建文档时收到以下错误。

/Users/Ben/Documents/Python/clearplot/doc/source/api/clearplot.rst:19: WARNING: autodoc: failed to import module u'clearplot'; the following exception was raised:
Traceback (most recent call last):
File "/Users/Ben/Library/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/sphinx/ext/autodoc.py", line 385, in import_object
__import__(self.modname)
File "/Users/Ben/Documents/Python/clearplot/clearplot/__init__.py", line 45, in <module>
import params
File "/Users/Ben/Documents/Python/clearplot/clearplot/params.py", line 19, in <module>
_mpl.rcParams['backend'] = 'Qt4Agg'
TypeError: '_MockModule' object does not support item assignment

如果我注释掉有问题的行 ( _mpl.rcParams['backend'] = 'Qt4Agg'),我会在下一行得到相同的错误,因为我的params模块充满了对matplotlib.rcParams字典的分配。

这两个问题看起来都很难解决。有没有更好的办法?如果没有,我应该如何解决问题 #1 和 #2?

从阅读本文来看,似乎有一种解决方法可以将字典与模拟一起使用。我应该寻找一个类似的解决方案来使用像来自的字符串一样基本的东西matplotlib.__version__吗?

如果有帮助,github 上的代码在这里

4

0 回答 0