我尝试在windows 10上通过Pycharm IDE使用matplotlib包。 当我运行这段代码时:

from matplotlib import pyplot

我得到以下错误:

ImportError: No module named 'tkinter'

我知道在python2中。它被称为Tkinter,但这不是问题-我只是安装了一个全新的python 3.5.1。

编辑:此外,我还尝试导入'tkinter'和'tkinter' -这两个都不工作(都返回了我提到的错误消息)。


当前回答

有时(例如在osgeo4w分布中)tkinter被删除。

尝试更改matplotlib后端编辑位于[python install dir]/matplotlib/mpl-data/matplotlibrc中的matplotlibrc文件,将后端参数从backend: TkAgg更改为其他诸如backend: Qt4Aggas的内容,此处描述:http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

其他回答

对于windows用户,重新运行安装程序。选择修改。选中tcl/tk和IDLE复选框。它的描述是“安装tkinter”

在CentOS 6.5和python 2.7上,我需要做:yum安装python27-tkinter

有时(例如在osgeo4w分布中)tkinter被删除。

尝试更改matplotlib后端编辑位于[python install dir]/matplotlib/mpl-data/matplotlibrc中的matplotlibrc文件,将后端参数从backend: TkAgg更改为其他诸如backend: Qt4Aggas的内容,此处描述:http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

为像我这样的穷人使用python 3.7。您需要python3.7-tk包。

安装python3.7-tk

$ python
Python 3.7.4 (default, Sep  2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ModuleNotFoundError: No module named 'tkinter'
>>> exit()

请注意。安装Python3-tk。但不是python3.7-tk。

$ sudo apt install python3.7-tk
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
  tix python3.7-tk-dbg
The following NEW packages will be installed:
  python3.7-tk
0 upgraded, 1 newly installed, 0 to remove and 34 not upgraded.
Need to get 143 kB of archives.
After this operation, 534 kB of additional disk space will be used.
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu xenial/main amd64 python3.7-tk amd64 3.7.4-1+xenial2 [143
kB]
Fetched 143 kB in 0s (364 kB/s)
Selecting previously unselected package python3.7-tk:amd64.
(Reading database ... 256375 files and directories currently installed.)
Preparing to unpack .../python3.7-tk_3.7.4-1+xenial2_amd64.deb ...
Unpacking python3.7-tk:amd64 (3.7.4-1+xenial2) ...
Setting up python3.7-tk:amd64 (3.7.4-1+xenial2) ...

安装之后,一切都好了。

$ python3
Python 3.7.4 (default, Sep  2 2019, 20:44:09)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tkinter
>>> exit()

在2018年初的Ubuntu上,Ubuntu (xenial/16.04)的正态分布中没有python3.6-tk,所以即使你有早期版本的python-tk也不能工作。

我的解决方案是使用python 3.5设置所有内容:

 sudo apt install python3.5-tk
 virtualenv --python=`which python3.5` python-env
 source python-env/bin/activate
 pip install -r requirements.txt

现在matplotlib可以找到tkinter。

编辑:

毕竟我只需要3.6个,诀窍是:

sudo apt install tk-dev

然后在tk-dev之后重新构建python3.6,例如:

./configure
make
make install