Home Backend Development Python Tutorial Python升级提示Tkinter模块找不到的解决方法

Python升级提示Tkinter模块找不到的解决方法

Jun 16, 2016 am 08:42 AM
python upgrade

一、安装tkinter
在Linux中python默认是不安装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
Traceback (most recent call last):
  File "", line 1, in
ImportError: No module named Tkinter
>>>
我们安装Tkinter模块
复制代码 代码如下:

[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版本
复制代码 代码如下:

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

DOWN新版本
复制代码 代码如下:

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

解压安装
复制代码 代码如下:

[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?
复制代码 代码如下:

[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 "", line 1, in
  File "/root/Python-2.7.4/Lib/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
>>>

提示找不到tkinter模块?看看旧版的是不是正常
复制代码 代码如下:

[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一次?
复制代码 代码如下:

[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模块在哪里?
复制代码 代码如下:

[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文件
复制代码 代码如下:

[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版本修改
复制代码 代码如下:

[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
复制代码 代码如下:

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

开始配置安装
复制代码 代码如下:

[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模块
复制代码 代码如下:

[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
>>>
已经没问题,旧版再看看
复制代码 代码如下:

[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命令路径
复制代码 代码如下:

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

cp 过去
复制代码 代码如下:

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

四、升级Python引起yum版本无法使用的问题解决
不少童鞋安装后就
复制代码 代码如下:
cp python /usr/bin/python
导致yum时就提示
复制代码 代码如下:

[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默认版本,大家的默认版本是多少请按实际情况修改即可
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to Use Python to Find the Zipf Distribution of a Text File How to Use Python to Find the Zipf Distribution of a Text File Mar 05, 2025 am 09:58 AM

This tutorial demonstrates how to use Python to process the statistical concept of Zipf's law and demonstrates the efficiency of Python's reading and sorting large text files when processing the law. You may be wondering what the term Zipf distribution means. To understand this term, we first need to define Zipf's law. Don't worry, I'll try to simplify the instructions. Zipf's Law Zipf's law simply means: in a large natural language corpus, the most frequently occurring words appear about twice as frequently as the second frequent words, three times as the third frequent words, four times as the fourth frequent words, and so on. Let's look at an example. If you look at the Brown corpus in American English, you will notice that the most frequent word is "th

How Do I Use Beautiful Soup to Parse HTML? How Do I Use Beautiful Soup to Parse HTML? Mar 10, 2025 pm 06:54 PM

This article explains how to use Beautiful Soup, a Python library, to parse HTML. It details common methods like find(), find_all(), select(), and get_text() for data extraction, handling of diverse HTML structures and errors, and alternatives (Sel

Image Filtering in Python Image Filtering in Python Mar 03, 2025 am 09:44 AM

Dealing with noisy images is a common problem, especially with mobile phone or low-resolution camera photos. This tutorial explores image filtering techniques in Python using OpenCV to tackle this issue. Image Filtering: A Powerful Tool Image filter

How to Perform Deep Learning with TensorFlow or PyTorch? How to Perform Deep Learning with TensorFlow or PyTorch? Mar 10, 2025 pm 06:52 PM

This article compares TensorFlow and PyTorch for deep learning. It details the steps involved: data preparation, model building, training, evaluation, and deployment. Key differences between the frameworks, particularly regarding computational grap

Introduction to Parallel and Concurrent Programming in Python Introduction to Parallel and Concurrent Programming in Python Mar 03, 2025 am 10:32 AM

Python, a favorite for data science and processing, offers a rich ecosystem for high-performance computing. However, parallel programming in Python presents unique challenges. This tutorial explores these challenges, focusing on the Global Interprete

How to Implement Your Own Data Structure in Python How to Implement Your Own Data Structure in Python Mar 03, 2025 am 09:28 AM

This tutorial demonstrates creating a custom pipeline data structure in Python 3, leveraging classes and operator overloading for enhanced functionality. The pipeline's flexibility lies in its ability to apply a series of functions to a data set, ge

Serialization and Deserialization of Python Objects: Part 1 Serialization and Deserialization of Python Objects: Part 1 Mar 08, 2025 am 09:39 AM

Serialization and deserialization of Python objects are key aspects of any non-trivial program. If you save something to a Python file, you do object serialization and deserialization if you read the configuration file, or if you respond to an HTTP request. In a sense, serialization and deserialization are the most boring things in the world. Who cares about all these formats and protocols? You want to persist or stream some Python objects and retrieve them in full at a later time. This is a great way to see the world on a conceptual level. However, on a practical level, the serialization scheme, format or protocol you choose may determine the speed, security, freedom of maintenance status, and other aspects of the program

Mathematical Modules in Python: Statistics Mathematical Modules in Python: Statistics Mar 09, 2025 am 11:40 AM

Python's statistics module provides powerful data statistical analysis capabilities to help us quickly understand the overall characteristics of data, such as biostatistics and business analysis. Instead of looking at data points one by one, just look at statistics such as mean or variance to discover trends and features in the original data that may be ignored, and compare large datasets more easily and effectively. This tutorial will explain how to calculate the mean and measure the degree of dispersion of the dataset. Unless otherwise stated, all functions in this module support the calculation of the mean() function instead of simply summing the average. Floating point numbers can also be used. import random import statistics from fracti

See all articles