感觉python3-tk就是个坑,网上的教程看了一大堆,方法也是千奇百怪,估计每个人遇到情况都可能不太一样。以下就将自己的经历贴出来,以供大家参考。
基本上都一样,如下所示
Traceback (most recent call last):
File "/home/hadoop/PycharmProjects/wjw/ftdd/ftdd/fdfds2011.py", line 9, in <module>
import matplotlib.pyplot as plt
File "/home/hadoop/snap/pycharm-community/venv/lib/python3.5/site-packages/matplotlib/pyplot.py", line 2374, in <module>
switch_backend(rcParams["backend"])
File "/home/hadoop/snap/pycharm-community/venv/lib/python3.5/site-packages/matplotlib/pyplot.py", line 207, in switch_backend
backend_mod = importlib.import_module(backend_name)
File "/usr/lib/python3.5/importlib/__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/hadoop/snap/pycharm-community/venv/lib/python3.5/site-packages/matplotlib/backends/backend_tkagg.py", line 1, in <module>
from . import _backend_tk
File "/home/hadoop/snap/pycharm-community/venv/lib/python3.5/site-packages/matplotlib/backends/_backend_tk.py", line 5, in <module>
import tkinter as Tk
File "/usr/lib/python3.5/tkinter/__init__.py", line 38, in <module>
raise ImportError(str(msg) + ', please install the python3-tk package')
ImportError: No module named '_tkinter', please install the python3-tk package
提示要安装python3-tk,接下来就是各种惊喜了…
开始以为是python自带的库,需要用pip安装。结果搜了一下,竟然没有!
于是网上看了下,大部分都是如下说
tkinter是一个python的接口类库,用以调用tcl/tk程序,故一般在操作系统层面会有相应的类库安装,而非仅仅依靠pip3来安装相应的python类库,比如, python3-tk。
那就直接在系统里安装 python3-tk
sudo apt-get install python3-tk
没有成功,提示如下:
The following packages have unmet dependencies:
gnupg : Breaks: software-properties-common (<= 0.96.24.3) but 0.96.20.7 is to be installed
E: Error, pkgProblemResolver::Resolve generated breaks, this may be caused by held packages.
网上查了下,说是软件包的依赖问题,要重新安装一下依赖包,输入如下代码:
sudo apt-get install software-properties-common=0.96.24.3
结果没有这个版本的包
E: Version '0.96.24.3' for 'software-properties-common' was not found
网上找了一下,发现有software-properties-common (0.96.24.32.1) ,于是重试了一下:
sudo apt-get install software-properties-common=0.96.24.32.1
结果又出现了新的包依赖问题
The following packages have unmet dependencies:
software-properties-common : Depends: python3-software-properties (= 0.96.24.32.1) but 0.96.20.7 is to be installed
E: Unable to correct problems, you have held broken packages.
网上的建议主要有两种:
我都试了一下。
先用大家说的aptitude工具试一试:
sudo apt install aptitude
sudo aptitude install python3-tk
结果是这样:
The following NEW packages will be installed:
blt{a} python3-tk python3.6{a} python3.6-minimal{a} tk8.6-blt2.5{a}
The following packages will be upgraded:
libpython3-stdlib python3 python3-minimal
3 packages upgraded, 5 newly installed, 0 to remove and 1645 not upgraded.
Need to get 677 kB/2,526 kB of archives. After unpacking 12.5 MB will be used.
The following packages have unmet dependencies:
onboard : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-systemd : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-reportlab-accel : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-gi-cairo : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-brlapi : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-pil : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
rhythmbox-plugins : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-uno : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-cups : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-renderpm : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-pycurl : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-cryptography : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
hplip : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-cffi-backend : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-markupsafe : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
totem-plugins : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-dbus : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-cairo : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-gi : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-lxml : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-gdbm : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
python3-apt : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
rhythmbox : Depends: python3 (< 3.6) but 3.6.5-3ubuntu1 is to be installed
The following actions will resolve these dependencies:
Keep the following packages at their current version:
1) libpython3-stdlib [3.5.1-3 (now)]
2) python3 [3.5.1-3 (now)]
3) python3-minimal [3.5.1-3 (now)]
4) python3-tk [Not Installed]
问我接不接受,我当然不接受,准备采用第二种方案,结果
Accept this solution? [Y/n/q/?] n
open: 6453; closed: 16494; defer: 28; conflict: 39 oNo solution found within the allotted time. Try harder? [Y/n] Y
Resolving dependencies...
open: 13428; closed: 34395; defer: 31; conflict: 43 ONo solution found within the allotted time. Try harder? [Y/n] Y
Resolving dependencies...
open: 20374; closed: 52401; defer: 32; conflict: 44 .No solution found within the allotted time. Try harder? [Y/n] n
没有第二种方案???
还出现了这么多新的包依赖问题,在网上查了一下“The following packages have unmet dependencies”这个问题,看到推荐的方法是用“sudo apt-get -f install”来修复,再试一下,结果如下
hadoop@ubun:~$ sudo apt-get install -f
[sudo] password for hadoop:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-4.15.0-29 linux-headers-4.15.0-29-generic linux-image-4.15.0-29-generic linux-modules-4.15.0-29-generic
Use 'sudo apt autoremove' to remove them.
0 upgraded, 0 newly installed, 0 to remove and 1633 not upgraded.
再运行一下程序,还是bug,不能解决问题。
参考教程,换了阿里的源,用 sudo vim /etc/apt/sources.list 覆盖了原文件后,执行
$ sudo apt-get update
过程中没出现什么错误,接着执行
$ sudo apt-get install python3-tk
出现如下提示
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
libpython3.6-minimal linux-headers-4.15.0-29 linux-headers-4.15.0-29-generic linux-image-4.15.0-29-generic linux-modules-4.15.0-29-generic vim-runtime
Use 'sudo apt autoremove' to remove them.
The following additional packages will be installed:
blt tk8.6-blt2.5
Suggested packages:
blt-demo tix python3-tk-dbg
The following packages will be REMOVED:
libpython3.6 libpython3.6-stdlib vim
The following NEW packages will be installed:
blt python3-tk tk8.6-blt2.5
0 upgraded, 3 newly installed, 3 to remove and 52 not upgraded.
Need to get 604 kB of archives.
After this operation, 13.5 MB disk space will be freed.
Do you want to continue? [Y/n]
确认执行后,没有出现什么问题,将后面的内容一并贴出来
Do you want to continue? [Y/n] y
Get:2 http://mirrors.aliyun.com/ubuntu xenial/main amd64 blt amd64 2.5.3+dfsg-3 [4,852 B]
Get:1 http://172.21.0.162/files/2063000000025039/us.archive.ubuntu.com/ubuntu xenial/main amd64 tk8.6-blt2.5 amd64 2.5.3+dfsg-3 [574 kB]
Get:3 http://mirrors.aliyun.com/ubuntu xenial/main amd64 python3-tk amd64 3.5.1-1 [25.1 kB]
Fetched 604 kB in 0s (2,033 kB/s)
(Reading database ... 266539 files and directories currently installed.)
Removing vim (2:8.0.1453-1ubuntu1) ...
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/vi (vi) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/view (view) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/ex (ex) in auto mode
update-alternatives: using /usr/bin/vim.tiny to provide /usr/bin/rview (rview) in auto mode
Removing libpython3.6:amd64 (3.6.6-1~18.04) ...
Removing libpython3.6-stdlib:amd64 (3.6.6-1~18.04) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/cuda-9.2/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link
Selecting previously unselected package tk8.6-blt2.5.
(Reading database ... 266175 files and directories currently installed.)
Preparing to unpack .../tk8.6-blt2.5_2.5.3+dfsg-3_amd64.deb ...
Unpacking tk8.6-blt2.5 (2.5.3+dfsg-3) ...
Selecting previously unselected package blt.
Preparing to unpack .../blt_2.5.3+dfsg-3_amd64.deb ...
Unpacking blt (2.5.3+dfsg-3) ...
Selecting previously unselected package python3-tk.
Preparing to unpack .../python3-tk_3.5.1-1_amd64.deb ...
Unpacking python3-tk (3.5.1-1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/cuda-9.2/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link
Setting up tk8.6-blt2.5 (2.5.3+dfsg-3) ...
Setting up blt (2.5.3+dfsg-3) ...
Setting up python3-tk (3.5.1-1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
/sbin/ldconfig.real: /usr/local/cuda-9.2/targets/x86_64-linux/lib/libcudnn.so.7 is not a symbolic link
hadoop@ubun:/etc/apt/sources.list.d$
最后,运行一下python代码,终于OK了!
(开心一秒,谁知道下个bug是什么呢…)