Linux升级Python提示Tkinter模块找不到解决[转载]

Linux升级Python提示Tkinter模块找不到解决

Python  2013-04-12  python,升级,tkinter

一、安装tkinter

在Linux中python默认是不安装Tkinter模块,

1
2
3
4
5
6
7
8
9
[root@li250-193 ~] # python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import Tkinter
Traceback (most recent call last):
   File "<stdin>" , line 1, in <module>
ImportError: No module named Tkinter
>>>

我们安装Tkinter模块

1
2
3
4
5
6
7
8
[root@li250-193 ~] # yum -y install tkinter
...
[root@li250-193 ~] # python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import Tkinter
>>>

二、升级Python

Linux的Python版本默认都不叫低

查看Python版本

1
2
[root@li250-193 ~] # python -V
Python 2.6.6

DOWN新版本

1
[root@li250-193 ~] # wget http://www.python.org/ftp/python/2.7.4/Python-2.7.4.tgz

解压安装

1
2
3
4
5
6
7
8
[root@li250-193 ~] # tar -xf Python-2.7.4.tgz
[root@li250-193 ~] # cd Python-2.7.4
[root@li250-193 Python-2.7.4] # ./configure
...
[root@li250-193 Python-2.7.4] # make
...
[root@li250-193 Python-2.7.4] # make install
...

看看新版本Python是否可以使用Tkinter?

1
2
3
4
5
6
7
8
9
10
11
[root@li250-193 Python-2.7.4] # ./python
Python 2.7.4 (default, Apr 12 2013, 08:03:09)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import Tkinter      
Traceback (most recent call last):
   File "<stdin>" , line 1, in <module>
   File "/root/Python-2.7.4/Lib/lib-tk/Tkinter.py" , line 39, in <module>
     import _tkinter # If this fails your Python may not be configured for Tk
ImportError: No module named _tkinter
>>>

提示找不到tkinter模块?看看旧版的是不是正常

1
2
3
4
5
6
[root@li250-193 Python-2.7.4] # python
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import Tkinter
>>>

旧版的没问题,难道需要yum install tkinter一次?

1
2
3
4
5
6
7
8
9
[root@li250-193 Python-2.7.4] # yum install tkinter
Loaded plugins: fastestmirror, security
Loading mirror speeds from cached hostfile
  * base: mirror.team-cymru.org
  * extras: mirror.team-cymru.org
  * updates: mirror.team-cymru.org
Setting up Install Process
Package tkinter-2.6.6-36.el6.x86_64 already installed and latest version
Nothing to do

提示已安装,看来不是tkinter的问题,看看tkinter模块在哪里?

1
2
[root@li250-193 Python-2.7.4] # find /usr -name *tkinter.so
/usr/lib64/python2 .6 /lib-dynload/_tkinter .so

找到一个,在2.6旧版本的目录下,估计是因为新版本库指向问题。于是认真读了README说明。重新配置安装

三、正确安装新版Python

首先修改Setup.dist文件

1
[root@li250-193 Python-2.7.4] # vim Modules/Setup.dist

找到下面这几行,把前面的井号去掉打开它

_tkinter _tkinter.c tkappinit.c -DWITH_APPINIT \
-L/usr/local/lib \
-I/usr/local/include \
-ltk8.5 -ltcl8.5 \
-lX11

以上第四行

-ltk8.5 -ltcl8.5 默认是 8.2 ,请你系统实际tcl/tk版本修改

1
2
3
4
5
[root@li250-193 Python-2.7.4] # rpm -qa | grep ^tk
tk-8.5.7-5.el6.x86_64
tkinter-2.6.6-36.el6.x86_64
[root@li250-193 Python-2.7.4] # rpm -qa | grep ^tcl
tcl-8.5.7-6.el6.x86_64

我系统中装的是8.5,所以这里我改成了8.5

保存退出

安装tck-devel、tk-devel

1
[root@li250-193 Python-2.7.4] # yum -y install tcl-devel tk-devel

开始配置安装

1
2
3
4
5
6
7
[root@li250-193 Python-2.7.4] # ldconfig
[root@li250-193 Python-2.7.4] # ./configure
...
[root@li250-193 Python-2.7.4] # make
...
[root@li250-193 Python-2.7.4] # make install
...

看下新版Python是否可以使用tkinter模块

1
2
3
4
5
6
[root@li250-193 Python-2.7.4] # ./python
Python 2.7.4 (default, Apr 12 2013, 08:49:11)
[GCC 4.4.6 20120305 (Red Hat 4.4.6-4)] on linux2
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import Tkinter
>>>

已经没问题,旧版再看看

1
2
3
4
5
6
[root@li250-193 Python-2.7.4] # /usr/bin/python2.6
Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2
Type "help" , "copyright" , "credits" or "license" for more information.
>>> import Tkinter
>>>

也没问题

如果直接敲入python -V查看版本是不是最新的,如果不是可以这么干:

which出python命令路径

1
2
[root@li250-193 Python-2.7.4] # which python
/usr/local/bin/python

cp 过去

1
[root@li250-193 Python-2.7.4] # cp python /usr/local/bin/python

四、升级Python引起yum版本无法使用的问题解决

不少童鞋安装后就

cp python /usr/bin/python

导致yum时就提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@lee ~] # yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:
 
    No module named yum
 
Please install a package which provides this module, or
verify that the module is installed correctly.
 
It 's possible that the above module doesn' t match the
current version of Python, which is:
2.7.4 (default, Apr  9 2013, 17:12:56)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-3)]
 
If you cannot solve this problem yourself, please go to
the yum faq at:
   http: //yum .baseurl.org /wiki/Faq
   
 
[root@lee ~] #

因为yum头部默认制定python脚本的路径就是

#! /usr/bin/python

你把旧版的python替换后就是用不了,不知道为何新版Python不能被yum识别,目前唯一最好解决的方法就是修改yum头部声明

改成

#! /usr/bin/python2.6

即可,这里的python2.6是我centos默认版本,大家的默认版本是多少请按实际情况修改即可






转载于:https://www.cnblogs.com/iathena/p/30b3f41554bbe1609656a7dff6911069.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要安装 Tkinter 库,你可以按照以下步骤进行操作: 1. 首先,确保你已经安装了 PythonTkinterPython 的标准库,通常情况下,它会随着 Python 一起安装。 2. 如果你使用的是 Python 2.x 版本,那么 Tkinter 库的名称是 `Tkinter`,如果你使用的是 Python 3.x 版本,那么 Tkinter 库的名称是 `tkinter`(注意大小写)。请根据你使用的 Python 版本使用正确的库名称。 3. 如果你没有安装 Python 的话,可以去官方网站 https://www.python.org/downloads/ 下载并安装最新版本的 Python。 4. 在安装完 Python 后,打开终端(Windows 使用命令提示符或 Power Shell),运行以下命令来检查是否已经安装了 Tkinter 库: ``` python -m tkinter ``` 5. 如果 Tkinter 已经安装,会打开一个空白窗口。这意味着你已经成功安装了 Tkinter 库。如果没有打开窗口或者提示不到模块,则说明你需要手动安装 Tkinter。 6. 对于 Linux 和 macOS 用户,可以尝试使用包管理工具来安装 Tkinter。例如,在 Ubuntu 上,可以运行以下命令来安装: ``` sudo apt-get install python-tk ``` 对于 macOS 上的 Homebrew 用户,可以使用以下命令来安装: ``` brew install python-tk ``` 7. 如果你使用的是 Windows 操作系统,可以尝试使用 pip 命令来安装 Tkinter。打开命令提示符或 Power Shell,运行以下命令: ``` pip install tk ``` 这将使用 pip 安装 Tkinter 库。 完成以上步骤后,你就可以在 Python 代码中使用 Tkinter 库了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值