Sphinx自动文档在readthedocs上出现问题:ImportError: No module named _tkinter。

5

我想要建立关于 https://readthedocs.org/ 的文档。

对于任何导入matplotlib的文件,我都看不到docstrings。

当我查看构建日志时,我发现from matplotlib import pyplot as plt失败了,并显示以下消息:

/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/checkouts/latest/docs/source/plotting.rst:67: WARNING: autodoc: failed to import function u'dbplot' from module u'artemis.plotting.db_plotting'; the following exception was raised:
Traceback (most recent call last):
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/sphinx/ext/autodoc.py", line 551, in import_object
    __import__(self.modname)
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/artemis_ml-1.6-py2.7.egg/artemis/plotting/db_plotting.py", line 3, in <module>
    from artemis.plotting.matplotlib_backend import BarPlot
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/artemis_ml-1.6-py2.7.egg/artemis/plotting/matplotlib_backend.py", line 7, in <module>
    from matplotlib import pyplot as plt
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/six.py", line 203, in load_module
    mod = mod._resolve()
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/home/docs/checkouts/readthedocs.org/user_builds/artemis-ml/envs/latest/lib/python2.7/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/home/docs/.pyenv/versions/2.7.13/lib/python2.7/lib-tk/Tkinter.py", line 39, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter

我已经在readthedocs的“管理员>高级设置”页面中指定了以下要求:
  • 项目应该在virtualenv中构建
  • 项目应该使用包含matplotlib的requirements.txt文件进行构建。您可以从上面的消息中看到它已经被安装了,只是显然安装不正确。
有人遇到过这种情况并知道如何解决吗?
由于这是关于readthedocs的问题,所以Tkinter: "Python may not be configured for Tk"中的答案对此无效。
3个回答

7

3

MERose提供的解决方案可能是最好的,但如果不起作用,这里还有另一种解决方法:

你可以通过使用“agg”后端让sphinx使用import matplotlib,而不需要tkinter,只需打开<project root>/docs/source/conf.py并插入以下行:

import matplotlib
matplotlib.use('agg')

在此结束。

0

导致导入错误的包需要在您的计算机上安装!我尝试在另一台电脑上创建文档,但该电脑上并未安装我的项目中包含的所有软件包,这当然会引发导入错误。

也许我只是在这里指出了显而易见的问题,但我花了相当长的时间才弄清楚这是问题所在。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接