简体   繁体   English

readthedocs上的Sphinx autodoc:ImportError:没有名为_tkinter的模块

[英]Sphinx autodoc on readthedocs: ImportError: No module named _tkinter

I'm trying to build documentation on https://readthedocs.org/ . 我正在尝试在https://readthedocs.org/上构建文档。

I cannot see the docstrings for any files that import matplotlib. 我无法看到导入matplotlib的任何文件的文档字符串。

When I look into the build log, I see that from matplotlib import pyplot as plt failed, with the message: 当我查看构建日志时,我看到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

I've already specified (in the readthedocs Admin>Advanced Settings) page so that: 我已经指定(在readthedocs管理>高级设置中)页面,以便:

  • The project should be build in a virtualenv 该项目应该建立在virtualenv
  • The project should build using the requirements.txt file which includes matplotlib. 该项目应使用包含matplotlib的requirements.txt文件构建。 You can also see that it's been installed from the above message - just not correctly apparently. 您还可以看到它已从上面的消息安装 - 只是显然不正确。

Anyone ran into this before and know how to fix it? 有人之前遇到过这个并且知道如何修复它吗?

The answers in Tkinter: "Python may not be configured for Tk" do not help as this is about readthedocs. Tkinter的答案:“可能没有为Tk配置Python”没有帮助,因为这是关于readthedocs。

You can solve this problem with a mock import. 您可以使用模拟导入来解决此问题。

Sphinx provides this features to autogenerate docs as decribed here . Sphinx提供此功能来自动生成此处所述的文档。 Simply write at the beginning of your conf.py 只需在conf.py开头写一下

autodoc_mock_imports = ['_tkinter']

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

MERose's solution is probably the best, but in case it doesn't work here is another workaround that worked for me: MERose的解决方案可能是最好的,但如果它不起作用,另一种解决方法对我有用:

You can just make sphinx use import matplotlib with the 'agg' backend, which does not require tkinter, by opening <project root>/docs/source/conf.py and inserting the lines: 你可以通过打开<project root>/docs/source/conf.py并插入行来让sphinx使用导入matplotlib和'agg'后端,它不需要<project root>/docs/source/conf.py

import matplotlib
matplotlib.use('agg')

At the end. 在末尾。

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

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