我尝试在windows 10上通过Pycharm IDE使用matplotlib包。
当我运行这段代码时:
from matplotlib import pyplot
我得到以下错误:
ImportError: No module named 'tkinter'
我知道在python2中。它被称为Tkinter,但这不是问题-我只是安装了一个全新的python 3.5.1。
编辑:此外,我还尝试导入'tkinter'和'tkinter' -这两个都不工作(都返回了我提到的错误消息)。
为像我这样的穷人使用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()
Linux
基于Debian的发行版:
sudo apt-get install python3-tk
基于RPM的发行版:
sudo yum install python3-tkinter
windows:
对于Windows,我认为问题是你没有安装完整的Python包。因为Tkinter应该与Python一起开箱发行。参见:http://www.tkdocs.com/tutorial/install.html。好的Windows python发行版可以在Anaconda或ActiveState公司找到。
测试python模块
python -c "import tkinter"
附注:我建议安装ipython,它提供了强大的shell和必要的包。
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()