ubuntu16中python3.6.8安裝tkinter

太長不看版

切回ubuntu16原始源,再安裝就可以了

問題記錄

再tensorflow中使用matplotlib的時候出現問題,圖形沒有出現,後面發現並不是tensorflow的問題

import matplotlib.pyplot as plt
plt.plot()
plt.show()

報錯:

UserWarning: Matplotlib is currently using agg, which is a non-GUI backend, so cannot show the figure.

搜索之後解決方案是在代碼中加入

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

之後又報錯

ModuleNotFoundError: No module named ‘tkinter’

之後就一連串曲折的tkinter安裝之路,各種pip3, apt報錯
根據各種報錯信息,終於定位到了問題需要安裝python3-tk

apt install python3-tk

此時報錯 python3-tk depends python(>=3.5) python(< 3.6) , 報錯信息沒記下,大概就是這麼個報錯。
此時終於看到曙光。

解決步驟

  1. 我的環境是 ubuntu 16 + python 3.6.8,ubuntu用的阿里雲的源。
  2. 經過查詢有建議通過 apt install python3.6-tk,安裝python3.6的tkinter。運行之後報錯找不到包。
  3. 切換清華的apt源,依然報錯。
  4. 切換回 ubuntu 原始源
apt update
apt install python3-tk

安裝成功,matplotlib繪圖問題解決。

切換apt原始源

編輯 vim /etc/apt/sources.list

# deb cdrom:[Ubuntu 16.04 LTS _Xenial Xerus_ - Release amd64 (20160420.1)]/ xenial main restricted

# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## universe WILL NOT receive any review or updates from the Ubuntu security
## team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial universe
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
# deb-src http://cn.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb http://archive.canonical.com/ubuntu xenial partner
# deb-src http://archive.canonical.com/ubuntu xenial partner

deb http://security.ubuntu.com/ubuntu xenial-security main restricted
# deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
# deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
# deb-src [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
# deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse
發佈了23 篇原創文章 · 獲贊 2 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章