【问题标题】:Python3 ImportError: No module named '_tkinter' on UbuntuPython3 ImportError:Ubuntu 上没有名为“_tkinter”的模块
【发布时间】:2017-10-29 10:57:28
【问题描述】:

Python3的当前版本是3.5.2,当我导入matplotlib时它重新调整了以下错误

>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/site-packages/matplotlib/pyplot.py", line 115, in <module>
    _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
    globals(),locals(),[backend_name],0)
  File "/usr/local/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 6, in <module>
    from six.moves import tkinter as Tk
  File "/usr/local/lib/python3.5/site-packages/six.py", line 92, in __get__
    result = self._resolve()
  File "/usr/local/lib/python3.5/site-packages/six.py", line 115, in _resolve
    return _import_module(self.mod)
  File "/usr/local/lib/python3.5/site-packages/six.py", line 82, in _import_module
    __import__(name)
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'

并导入 tkinter

Python 3.5.2 (default, Jan 19 2017, 11:29:22)
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.5/tkinter/__init__.py", line 35, in <module>
    import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named '_tkinter'
>>>

tkinter 好像已经安装好了。

我已经安装了 tk 和 tcl by

sudo apt-get install tk-dev
sudo apt-get install tk8.6-dev

操作系统是 Ubuntu 14.04。

我认为这是没有在 Python3 上配置 TK 的原因,但我不确定。很多人说我应该用 tk 重建和重新安装 Python3,但我认为这不是解决这个问题的优雅方法。

我该如何解决这个问题?

【问题讨论】:

  • 你试过用pip安装它吗?
  • @moritzg, Requirement already satisfied: tkinter in /usr/local/lib/python3.5/site-packages
  • 好的,sudo apt-get install python3-tk 呢?
  • @moritzg,之前已经安装过了
  • 检查你的路径..你的错误"/usr/local/lib/python3.5/tkinter/__init__.py"和你的安装Requirement already satisfied: tkinter in /usr/local/lib/python3.5/site-packages而不是它试图在python3.5中找到它的站点包再次配置你的tkinter路径

标签: python matplotlib tkinter


【解决方案1】:

如果您在使用 matplotlib 后端时遇到问题,请尝试选择其他后端。
Matplotlib 适用于许多不同的场景和用途。
在 Linux 上,我使用以下代码来选择哪个后端可用并且首先工作。

import matplotlib
gui_env = ['TKAgg','GTKAgg','Qt4Agg','WXAgg']
for gui in gui_env:
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        break
    except:
        continue

如果您要创建图像文件而不是显示它

用途:

matplotlib.use('agg')
from matplotlib import pyplot as plt

编辑:
根据你的 cmets 试试这个,看看你是否得到了有效的结果。

import matplotlib
gui_env = [i for i in matplotlib.rcsetup.interactive_bk]
print ("I will test for", gui_env)
for gui in gui_env:
    print ("testing", gui)
    try:
        matplotlib.use(gui,warn=False, force=True)
        from matplotlib import pyplot as plt
        print ("    ",gui, "Is Available")
        plt.plot([1.5,2.0,2.5])
        fig = plt.gcf()
        fig.suptitle(gui)
        plt.show()
        print ("Using ..... ",matplotlib.get_backend())
    except:
        print ("    ",gui, "Not found")

【讨论】:

  • 谢谢,但是它返回了ImportError: No module named 'wx' .... ImportError: Matplotlib backend_wx and backend_wxagg require wxPython &gt;=2.8.12
  • 换句话说,在您的系统上,您没有测试过 4 个常见的交互式后端中的一个!你不是在运行 Linux 吗?
  • 是的,Ubuntu Server 14.04。
  • 'WebAgg', 'nbAgg' 可用。
  • 你那里有一些疯狂的 linux 盒子,如果它们是唯一可用的 2 个的话
猜你喜欢
  • 1970-01-01
  • 2021-10-03
  • 1970-01-01
  • 2014-06-04
  • 1970-01-01
  • 2020-10-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多