问题:matplotlib错误-没有名为tkinter的模块

当我运行以下代码时,我尝试通过Windows 10上的Pycharm IDE使用matplotlib软件包:

from matplotlib import pyplot

我收到以下错误:

ImportError: No module named 'tkinter'

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

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

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

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.

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


回答 0

sudo apt-get install python3-tk

然后,

>> import tkinter # all fine

编辑

对于Windows,我认为问题是您没有安装完整的Python软件包。由于Tkinter应该随Python一起提供。请参阅:http : //www.tkdocs.com/tutorial/install.html

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

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

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


回答 1

您可以使用

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

如果您tkinter根本不想使用。

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

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

you can use

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

if you dont want to use tkinter at all.

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

EDIT: agg is a different backend like tkinter for matplotlib.


回答 2

在Centos上,软件包名称和命令是不同的。您需要执行以下操作:

sudo yum install tkinter

解决问题。

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

sudo yum install tkinter

To fix the problem.


回答 3

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

  1. 进入开始菜单,输入“ 程序和功能”
  2. 选择Python版本(对我来说是Python 3.6.5(64位)),
  3. 右键单击,按更改
  4. 点击修改
  5. 选择td / tk和IDLE(将安装tkinter),然后单击下一步

等待安装,您就完成了。

For Windows users, there’s no need to download the installer again. Just do the following:

  1. Go to start menu, type Apps & features,
  2. Search for “python” in the search box,
  3. Select the Python version (e.g. Python 3.8.3rc1(32-bit)) and click Modify,
  4. On the Modify Setup page click Modify,
  5. Tick td/tk and IDLE checkbox (which installs tkinter) and click next.

Wait for installation and you’re done.


回答 4

我搜索此问题的几乎所有答案都说Windows上的Python随附了tkinter和tcl,并且我没有运气尝试使用pip或actviestate.com网站下载或安装它们。我最终发现,当我使用二进制安装程序安装python时,我没有选中与TCL和tkinter相关的模块。因此,我再次运行二进制安装程序,并选择了这次选择此选项来修改我的python版本。然后,无需手动执行任何操作。如果您转到python终端,则以下命令应显示与Python一起安装的tkinter版本:

import tkinter
import _tkinter
tkinter._test()

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:

import tkinter
import _tkinter
tkinter._test()

回答 5

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

sudo dnf install python3-tkinter

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

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

并使用pip添加必要的软件包

If you are using fedora then first install tkinter

sudo dnf install python3-tkinter

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

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

And add the necessary packages using pip


回答 6

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

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

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

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


回答 7

对于Windows用户,请重新运行安装程序。选择修改。选中tcl / tk和IDLE框。此说明说“ Installs tkinter”

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


回答 8

在Ubuntu上,2018年初,python3.6-tkUbuntu的(xenial / 16.04)正态发行版上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

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

./configure
make
make install

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

My solution was to use set everything up with 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 matplotlib can find tkinter.

EDIT:

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

sudo apt install tk-dev

and then rebuild python3.6, after tk-dev, eg:

./configure
make
make install

回答 9

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

sudo apt-get install python3.6-tk

代替

sudo apt-get install python3-tk

适用于其他版本的python3

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

sudo apt-get install python3.6-tk

instead of

sudo apt-get install python3-tk

Which works for other versions of python3


回答 10

对于像我这样的可怜人,使用python 3.7。您需要python3.7-tk包装。

sudo apt install 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()

For the poor guys like me using python 3.7. You need the python3.7-tk package.

sudo apt install 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()

Note. python3-tk is installed. But not 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) ...

After installing it, all good.

$ 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()

回答 11

在带有python 2.7的CentOS 6.5上,我需要这样做: yum install python27-tkinter

On CentOS 6.5 with python 2.7 I needed to do: yum install python27-tkinter


回答 12

有时(例如在osgeo4w发行版中)tkinter被删除。

尝试改变matplotlib后台编辑matplotlibrc位于文件[python install dir]/matplotlib/mpl-data/matplotlibrc变化的后台参数来自backend: TkAgg于其他类似的东西backend: Qt4Agg如下所述:http://matplotlib.org/faq/usage_faq.html#what-is-a-backend

Sometimes (for example in osgeo4w distribution) tkinter is removed.

Try changing matplotlib backend editing matplotlibrc file located in [python install dir]/matplotlib/mpl-data/matplotlibrc changing The backend parameter from backend: TkAgg to something other like backend: Qt4Aggas described here: http://matplotlib.org/faq/usage_faq.html#what-is-a-backend


回答 13

由于我在Ubuntu上使用Python 3.7,因此必须使用:

sudo apt-get install python3.7-tk

Since I’m using Python 3.7 on Ubuntu I had to use:

sudo apt-get install python3.7-tk

回答 14

也许您从源代码安装了python。在这种情况下,您可以在支持tcl / tk的情况下重新编译python。

  1. 从以下位置编译并安装tcl / tk http://www.tcl.tk/software/tcltk/download.html,我假设您在安装了python /home/xxx/local/tcl-tk/
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install

# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
  1. 使用支持的tcl / tk重新编译python,例如:
# download the source code of python and decompress it first.

cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
 --with-tcltk-includes=/home/xxx/local/tcl-tk/include \
 --with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make 
make install

Maybe you installed python from source. In this case, you can recompile python with tcl/tk supported.

  1. Complie and install tcl/tk from http://www.tcl.tk/software/tcltk/download.html, I’ll suppose you installed python at /home/xxx/local/tcl-tk/.
# install tcl
wget -c https://prdownloads.sourceforge.net/tcl/tcl8.6.9-src.tar.gz
tar -xvzf tcl8.6.9-src.tar.gz
cd tcl8.6.9
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install

# install tk
wget -c https://prdownloads.sourceforge.net/tcl/tk8.6.9.1-src.tar.gz
tar -xvzf tk8.6.9.1-src.tar.gz
cd tk8.6.9.1
./configure --prefix=/home/xxx/local/tcl-tk/
make
make install
  1. Recompile python with tcl/tk supported, for example:
# download the source code of python and decompress it first.

cd <your-python-src-dir>
./configure --prefix=/home/xxx/local/python \
 --with-tcltk-includes=/home/xxx/local/tcl-tk/include \
 --with-tcltk-libs=/home/xxx/local/tcl-tk/lib
make 
make install

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。