Solving ImportError: No module named Tkinter in python2.7 environment

background:

When using python2.7 that comes with Linux, an error occurs when importing Tkinter: ImportError: No module named Tkinter

>>> import Tkinter
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named Tkinter
>>> 

solve: 

When using sudo yum search tkinter to search the warehouse, I found the installation package python2-tkinter.

So, sudo yum install python2-tkinter.x86_64 was ok.

sudo yum install  python2-tkinter.x86_64

Notice:

The imported package in Python2 is import Tkinter

The imported package in python3 is import tkinter

Python installed from source code:

If python already exists before and you do not want to recompile and overwrite it, because after overwriting, all packages previously installed with pip will have to be reinstalled. Follow the steps below to install:

1. First check the encoding compiled by python before.
The following is the --enable-unicode=ucs4 compilation. 

>>> import sys
>>> print sys.maxunicode
1114111

The following is compiled with --enable-unicode=ucs2

>>> import sys
>>> print sys.maxunicode
65535

Start compiling after determining the compiled encoding. For example, my old version of python is compiled to the /usr/local/python2 directory, and my encoding is ucs2, then follow the following steps to start compiling:

进入python的tar包解压目录执行
./configure --prefix=/usr/local/python2 --enable-unicode=ucs2
make clean
make
make install

 Reference: python2.7 environment to solve ImportError: No module named _tkinter_python2 no module named tkinter_Passionate Mushroom’s Blog-CSDN Blog

 

Guess you like

Origin blog.csdn.net/m0_46829545/article/details/131435297