Python matplotlib 错误 - 没有名为 tkinter 的模块

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36327134/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 17:44:20  来源:igfitidea点击:

matplotlib error - no module named tkinter

pythonmatplotlibtkinter

提问by noamgot

I tried to use the matplotlib package via Pycharm IDE on windows 10. when I run this code:

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

from matplotlib import pyplot

I get the following error:

我收到以下错误:

ImportError: No module named 'tkinter'

I know that in python 2.x it was called Tkinter, but that is not the problem - I just installed a brand new python 3.5.1.

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

EDIT: in addition, I also tried to import 'tkinter' and 'Tkinter' - neither of these worked (both returned the error message I mentioned).

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

回答by knh170

sudo apt-get install python3-tk

Then,

然后,

>> import tkinter # all fine

Edit:

编辑

For Windows, I think the problem is you didn't install complete Python package. Since Tkinter should be shipped with Python out of box. See: http://www.tkdocs.com/tutorial/install.html

对于 Windows,我认为问题在于您没有安装完整的 Python 包。由于 Tkinter 应该与 Python 一起开箱即用。请参阅:http: //www.tkdocs.com/tutorial/install.html

I suggest install ipython, which provides powerful shell and necessary packages as well.

我建议安装ipython,它也提供了强大的 shell 和必要的包。

回答by markroxor

you can use

您可以使用

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

if you dont want to use tkinterat all.

如果你tkinter根本不想使用。

Also dont forget to use %matplotlib inlineat the top of your notebook if using one.

%matplotlib inline如果使用笔记本,也不要忘记在笔记本顶部使用。

EDIT: aggis a different backend like tkinterfor matplotlib.

编辑:agg是一个不同的后端,如tkintermatplotlib。

回答by razeh

On Centos, the package names and commands are different. You'll need to do:

在 Centos 上,包名和命令是不同的。你需要做:

sudo yum install tkinter

To fix the problem.

解决问题。

回答by gdrt

For Windowsusers, there's no need to download the installer again. Just do the following:

对于Windows用户,无需再次下载安装程序。只需执行以下操作:

  1. Go to start menu, type Programs and Features,
  2. Select the Python version (for me it's Python 3.6.5(64-bit)),
  3. Right click, press Change,
  4. Click Modify,
  5. Select td/tk and IDLE(which installs tkinter) and click next.
  1. 转到开始菜单,键入Programs and Features
  2. 选择 Python 版本(对我来说是Python 3.6.5(64-bit)),
  3. 右键单击,按Change
  4. 点击修改
  5. 选择td/tk 和 IDLE(安装 tkinter),然后单击下一步

Wait for installation and you're done.

等待安装,你就完成了。

回答by user58419

Almost all answers I searched for this issue say that Python on Windows comes with tkinter and tcl already installed, and I had no luck trying to download or install them using pip, or actviestate.com site. I eventually found that when I was installing python using the binary installer, I had unchecked the module related to TCL and tkinter. So, I ran the binary installer again and chose to modify my python version by this time selecting this option. No need to do anything manually then. If you go to your python terminal, then the following commands should show you version of tkinter installed with your Python:

我搜索过的几乎所有关于这个问题的答案都说 Windows 上的 Python 已经安装了 tkinter 和 tcl,我没有运气尝试使用 pip 或 actviestate.com 站点下载或安装它们。我最终发现,当我使用二进制安装程序安装 python 时,我取消了与 TCL 和 tkinter 相关的模块。因此,我再次运行二进制安装程序并选择通过这次选择此选项来修改我的 python 版本。无需手动执行任何操作。如果你去你的 python 终端,那么下面的命令应该显示你的 Python 安装的 tkinter 版本:

import tkinter
import _tkinter
tkinter._test()

回答by sage poudel

If you are using fedora then first install tkinter

如果您使用的是 Fedora,请先安装 tkinter

sudo dnf install python3-tkinter

I don't think you need to import tkinter afterwards I also suggest you to use virtualenv

我认为您之后不需要导入 tkinter 我也建议您使用 virtualenv

$ python3 -m venv myvenv
$ source myvenv/bin/activate

And add the necessary packages using pip

并使用 pip 添加必要的包

回答by Krzysztof Buchacz

On CentOS 7 and Python 3.4, the command is sudo yum install python34-tkinter

在 CentOS 7 和 Python 3.4 上,命令是 sudo yum install python34-tkinter

On Redhat 7.4 with Python 3.6, the command is sudo yum install rh-python36-python-tkinter

在带有 Python 3.6 的 Redhat 7.4 上,命令是 sudo yum install rh-python36-python-tkinter

回答by Josh.F

On Ubuntu, early 2018, there is no python3.6-tkon ubuntu's (xenial/16.04) normal distributions, so even if you have earlier versions of python-tkthis won't work.

在 2018 年初的 Ubuntu 上,python3.6-tk在 ubuntu 的 (xenial/16.04) 正态发行版上没有,所以即使你有早期版本python-tk也行不通。

My solution was to use set everything up with python 3.5:

我的解决方案是使用设置所有内容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

And now matplotlibcan find tkinter.

现在matplotlib可以找到tkinter.

EDIT:

编辑

I just needed 3.6 afterall, and the trick was to:

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

sudo apt install tk-dev

and then rebuild python3.6, aftertk-dev, eg:

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

./configure
make
make install

回答by petEEy

For windows users, re-run the installer. Select Modify. Check the box for tcl/tk and IDLE. The description for this says "Installs tkinter"

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

回答by bertucho

If you are using python 3.6, this worked for me:

如果您使用的是 python 3.6,这对我有用:

sudo apt-get install python3.6-tk

instead of

代替

sudo apt-get install python3-tk

Which works for other versions of python3

适用于其他版本的python3