【问题标题】:Sphinx autodoc on readthedocs: ImportError: No module named _tkinter关于 readthedocs 的 Sphinx 自动文档:ImportError: No module named _tkinter
【发布时间】:2018-01-11 01:13:21
【问题描述】:

我正在尝试在 https://readthedocs.org/ 上构建文档。

我看不到任何导入 matplotlib 的文件的文档字符串。

当我查看构建日志时,我看到 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 文件构建。您还可以从上面的消息中看到它已安装 - 只是显然不正确。

有人遇到过这个问题并知道如何解决吗?

Tkinter: "Python may not be configured for Tk" 中的答案无济于事,因为这是关于 readthedocs。

【问题讨论】:

    标签: python matplotlib tkinter read-the-docs


    【解决方案1】:

    您可以通过模拟导入来解决这个问题。

    Sphinx 提供此功能以自动生成文档,如 here 所述。只需写在conf.py的开头

    autodoc_mock_imports = ['_tkinter']
    

    这里有一个例子:https://github.com/scopus-api/scopus/blob/4bb7f62f7a94e652f77515c94e7e0bab0d07cce7/docs/conf.py#L24

    【讨论】:

      【解决方案2】:

      MERose 的解决方案可能是最好的,但如果它在这里不起作用,那么另一种解决方法对我有用:

      您可以通过打开 &lt;project root&gt;/docs/source/conf.py 并插入以下行来使 sphinx 使用带有 'agg' 后端的 import matplotlib,这不需要 tkinter:

      import matplotlib
      matplotlib.use('agg')
      

      最后。

      【讨论】:

        【解决方案3】:

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

        也许我只是在这里指出明显的问题,但我花了很长时间才弄清楚这是问题所在。

        【讨论】:

          猜你喜欢
          • 2018-10-23
          • 1970-01-01
          • 1970-01-01
          • 2015-09-10
          • 2012-08-12
          • 2011-10-19
          • 2018-01-19
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多