python2.7环境解决ImportError: No module named _tkinter

  • Post author:
  • Post category:python


  1. 首先安装Tkinter模块:yum -y install tkinter

  2. 再次运行import Tkinter后报错:

    Python代码

    Type “help”, “copyright”, “credits” or “license” for more information.

    import Tkinter

    Traceback (most recent call last):

    File “”, line 1, in

    File “/usr/local/python27/lib/python2.7/lib-tk/Tkinter.py”, line 39, in

    import _tkinter # If this fails your Python may not be configured for Tk

    ImportError: No module named _tkinter

  3. 运行yum install tk-devel tcl-devel ;

  4. 下载python2.7.9后,在编译前先在/usr/local建一个文件夹mkdir /usr/local/python27,使用tar zxvf test.tgz -C dir解压python2.7.9,

    在解压后的目录下运行./configure –prefix=/usr/local/python27 –enable-unicode=ucs4,通过make&make install编译;

    Shell代码
     mkdir /usr/local/python27
     ./configure --prefix=/usr/local/python27 --enable-unicode=ucs4
     make clean
     make
     make install
     重新编译python2.7.9。
    

5.建立新版本python的链接ln -s /usr/local/python27/bin/python2.7 /usr/bin/python,运行python的话即可看到引入新版本python2.7.9。再试用import Tkinter,完成。


ps:如果之前已经存在python,不想重新编译覆盖,因为覆盖后之前用pip安装的包都要重新安装,按以下步骤安装


1、先查看之前python编译的编码

以下是 –enable-unicode=ucs4 编译

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

以下是 –enable-unicode=ucs2 编译

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

确定编译的编码后开始编译,比如我旧版python是编译到/usr/local/python2目录,且我的编码是ucs2,则按以下步骤开始编译:

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



版权声明:本文为sunjinjuan原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。