代码之家  ›  专栏  ›  技术社区  ›  Peter

readthedocs上的Sphinx autodoc:ImportError:没有名为\u tkinter的模块

  •  5
  • Peter  · 技术社区  · 7 年前

    我正在努力建立文档 https://readthedocs.org/ .

    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 Admin>Advanced Settings)页面,以便:

    • 项目应该使用 requirements.txt

    答案在 Tkinter: "Python may not be configured for Tk" 不要帮忙,因为这是关于readthedocs的。

    3 回复  |  直到 7 年前
        1
  •  7
  •   Florian Brucker    6 年前

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

    Sphinx提供了这些功能来自动生成所描述的文档 here conf.py

    autodoc_mock_imports = ['_tkinter']
    

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

        2
  •  3
  •   Peter    7 年前

    <project root>/docs/source/conf.py

    import matplotlib
    matplotlib.use('agg')
    

    最后。

        3
  •  0
  •   Tun Kapgen    3 年前

    产生导入错误的软件包需要安装在您的计算机上!我试图在另一台电脑上创建文档,我的项目中并不是所有的软件包都安装在这台电脑上,当然这会导致导入错误。

    也许我只是在这里指出显而易见的问题,但我花了很长时间才发现这就是问题所在。