2016-09-21 59 views
0

我在用python3.5運行matplotlib.pyplot和tkinter時遇到了麻煩。我在Ubuntu 12.04.5 LTS上。當我輸入dpkg -l python3-tk時,我看到安裝了python3-tk。我應該如何排除故障?用Tkinter和python3.5導入錯誤

python3-tk僅適用於python3.2而不適用python3.5嗎?

實例下面python3.2與python3.5安裝

machine:~$ /usr/bin/python3 
Python 3.2.3 (default, Jun 18 2015, 21:46:58) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tkinter 
>>> exit() 

machine:~$ /usr/bin/python3.5 
Python 3.5.2 (default, Jul 17 2016, 17:38:18) 
[GCC 4.6.3] on linux 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import tkinter 
Traceback (most recent call last): 
    File "/usr/lib/python3.5/tkinter/__init__.py", line 36, in <module> 
    import _tkinter 
ImportError: No module named '_tkinter' 

During handling of the above exception, another exception occurred: 

Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    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

machine:~$ dpkg -l python3-tk 
Desired=Unknown/Install/Remove/Purge/Hold 
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend 
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) 
||/ Name       Version      Description 
    +++-============================-============================- 

======================================================================== 
ii python3-tk     3.2.3-1      Tkinter - Writing Tk applications with Python 3.x 
+0

我懷疑_tkinter.so,編譯自_tkinter.c,位於/ usr/bin/python3中,並且它也需要位於/usr/bin/python3.5中的相應位置。是否需要重新編譯爲3.5(如Windows上的情況)或者僅僅是複製,我不知道。 –

回答

0

的python3-TK文庫3.2版本下安裝,而不是3.5。 Python3-TK卸載並安裝python3.5-tk的,而不是

1
sudo apt-get install python3-tk 

此命令將安裝的Tkinter作爲默認python3而已,所以python3.5導入錯誤。

您可以運行下面的命令來安裝python3.5

sudo apt-get install python3.5-tk 
0

更改matplotlib後端「AGG」或其他一些非交互式選項。像下面這樣:

matplotlib.use( 'AGG')

More info here(在碼頭工人,在那裏我遇到這個問題,併成功地解決了它的上下文中):