Tkinter和python3.5导入错误

Tkinter和python3.5导入错误,tkinter,ubuntu-12.04,python-3.5,Tkinter,Ubuntu 12.04,Python 3.5,使用Python 3.5运行matplotlib.pyplot和tkinter时遇到问题。我在Ubuntu 12.04.5 LTS上。我看到在键入dpkg-l python3tk时安装了python3tk。我还应该如何排除故障 python3 tk是否仅用于python3.2而不是python3.5 下面是python3.2与python3.5的对比示例 machine:~$ /usr/bin/python3 Python 3.2.3 (default, Jun 18 2015, 21:46:5

使用Python 3.5运行matplotlib.pyplot和tkinter时遇到问题。我在Ubuntu 12.04.5 LTS上。我看到在键入dpkg-l python3tk时安装了python3tk。我还应该如何排除故障

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
sudo apt-get install python3.5-tk

python3tk库是在3.2版本下安装的,而不是3.5版本。Python3-tk被卸载,而Python3.5-tk被安装

sudo apt-get install python3-tk
此命令将仅为默认的python3安装tkinter,因此python3.5将被导入

您可以运行以下命令来安装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
sudo apt-get install python3.5-tk

将matplotlib后端更改为“agg”或其他非交互式选项。如下所示:

matplotlib.use('agg')


(在Docker的环境中,我遇到了这个问题并成功地解决了它):

我尝试在ubuntu16.02中使用这个命令,它可以工作

sudo apt-get install python3.5-tk

默认情况下,我安装了python2.7。您可以参考这个

我怀疑从_tkinter.c编译的_tkinter.so在/usr/bin/python3中的某个地方,它还需要在/usr/bin/python3.5中的相应位置。我不知道是需要为3.5重新编译(就像在Windows上一样)还是仅仅复制。在我的例子中,我得到了错误E:包“python3.5-tk”没有安装候选程序。最后,我通过将Python3.5升级到3.6进行了修复。