centos安装python2.7

一、安装perl

[root@centos ~]# wget http://www.cpan.org/src/5.0/perl-5.16.0.tar.gz 

[root@centos ~]# tar zxvf perl-5.16.0.tar.gz 

[root@centos ~]# cd perl-5.16.0

[root@centos perl-5.16.0]# mkdir -p /usr/local/perl
[root@centos perl-5.16.0]# ls /usr/local/

编译参数使用默认的

[root@centos perl-5.16.0]# ./Configure -des -Dprefix=/usr/local/perl/

[root@centos perl-5.16.0]# make

[root@centos perl-5.16.0]# make test

[root@centos perl-5.16.0]# make install
到此安装结束,测试是否正常安装。

[root@centos ~]# perl -v
This is perl, v5.8.8 built for i386-linux-thread-multi
Copyright 1987-2006, Larry Wall
Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.
Complete documentation for Perl, including FAQ lists, should be found on
this system using “man perl” or “perldoc perl”.  If you have access to the
Internet, point your browser at http://www.perl.org/, the Perl Home Page.


二、安装python、及问题解决


[root@centos ~]# wget http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2

[root@centos ~]# tar jxvf Python-2.7.3.tar.bz2 

[root@centos ~]# cd Python-2.7.3

[root@centos perl-5.16.0]# mkdir -p /usr/local/python
[root@centos perl-5.16.0]# ls /usr/local/

[root@centos Python-2.7.3]# ./configure –prefix=/usr/local/python/

[root@centos Python-2.7.3]# make

遇到问题:

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _sqlite3           _tkinter        
bsddb185           bz2                dbm             
gdbm               readline           sunaudiodev     
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

Failed to build these modules:
_curses            _curses_panel   

由提示,先解决未编译成功模块_curses、_curses_panel   这是由于相关的库文件找不到引起的,安装ncurses可解决,但ncurses是比较常见的,此前应该安装过,系统里不应该没有,也不管了,使用yum安装。

[root@centos Python-2.7.3]# yum install ncurses ncurses-devel

再次make

[root@centos Python-2.7.3]# make

提示:

Python build finished, but the necessary bits to build these modules were not found:
_bsddb             _sqlite3           _tkinter        
bsddb185           bz2                dbm             
gdbm               readline           sunaudiodev     
To find the necessary bits, look in setup.py in detect_modules() for the module’s name.

由提示可以看出,之前的模块正常编译,但系统任提示缺少相关必须的位。这个问题困扰了我许久,在网上搜索了大量的资料,也没找到较好的解决办法,自己尝试安装一些相关的库,任然出现这个提示。但网上有些文档提到这个问题很常见,可忽略。自己尝试多次后未果,也只得忽略,后来证明没问题,但这也为我后来安装gitosis出错排错带来了麻烦。忽略之后直接安装。

ps:一些必要的模块还是要安装的,如果自己实在解决不了就大胆跳过,以后遇到问题,提示python某个必要的模块没安装再进行解决。

[root@centos Python-2.7.3]# make install

[root@centos Python-2.7.3]# python -V

Python 2.4.3

此时python版本任为2.4,这是因为centos系统中的python版本默认2.4。替代默认的版本:

[root@centos bin]# mv /usr/bin/python /usr/bin/python2.4           //可能python2.4已经存在

[root@centos bin]# ln -s /usr/local/python/bin/python /usr/bin/python

[root@centos bin]# which python
/usr/bin/python
[root@centos bin]# python -V
Python 2.7.3

升级安装好以后yum会无法使用,因为yum使用的是2.4版本,此时运行yum会有如下提示

[root@centos bin]# yum update
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.3 (default, Aug 12 2012, 20:23:42) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://wiki.linux.duke.edu/YumFaq

修改yum的配置文件以便能够识别2.4版本下的python

[root@centos bin]# vi /usr/bin/yum 
将 #!/usr/bin/python    修改为#!/usr/bin/python2.4

再次运行yum正常。

———————————-补充-2012-08-26———————————————

之前因为/usr/bin/ 已经有python2.4 所以将原本的python重命名为python2.4.1。修改/usr/bin/yum 是也改#!/usr/bin/python2.4
 运行yum正常,但每次开机提示:

Traceback (most recent call last): 
  File “/usr/lib/wicd/wicd-daemon.py”, line 46, in <module> 
    import dbus 
ImportError: No module named dbus 

后来修改/usr/bin/yum为#!/usr/bin/python2.4.1 开机正常,无该提示。

所以如果/usr/bin/ 已经有python2.4  mv时建议不要覆盖,但diff /usr/bin/python2.4
/usr/bin/python2.4.1无输出,重新修改/usr/bin/yum为#!/usr/bin/python2.4开机仍提示错误,再次留下疑问!

————————————–补充结束—————————————————-

——————————————————————————————————

这份文档已经结束了,最后在这里引用两个博客的文档作为自己的备忘:

一、http://www.cnblogs.com/lexus/archive/2012/02/23/2365660.html

Update:上面安装zlib的方法失败,原来Python 2.7得先安装zlib库:

cd /opt/Python-2.7.2/Modules/zlib
./configure make && make install

如此安装zlib

二、http://blog.sina.com.cn/s/blog_814b01e70100z6h7.html

这里,我们很清楚的可以看出,readline模块build失败了,我又查看是否build时有相关报错,我发现build过程有下列不明显的报错:

building ‘readline’ extension

gcc -fPIC -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3
-Wall -Wstrict-prototypes -I. -IInclude -I./Include -I/usr/local/include -I/tmp/Python-2.7.2/Include -I/tmp/Python-2.7.2 -c /tmp/Python-2.7.2/Modules/readline.c -o build/temp.solaris-2.10-sun4u-2.7/tmp/Python-2.7.2/Modules/readline.o

/tmp/Python-2.7.2/Modules/readline.c:758: warning:
‘on_completion_display_matches_hook’ defined but not used

gcc -shared build/temp.solaris-2.10-sun4u-2.7/tmp/Python-2.7.2/Modules/readline.o
-L/usr/lib/termcap -L/usr/local/lib -L. -lreadline -lncurses -lpython2.7 -o build/lib.solaris-2.10-sun4u-2.7/readline.so

Text relocation remains    

 

这个报错类似于python自己的module下,有它定义好的readline编译方式,它和我在config时,指定的readline有所冲突,不知道为什么,build时没法使用。虽然我在configure时,为此增加了很多参数,比如LDFLAGS,比如CPPFLAGS,还比如我visetup.py文件,在#
Platform-dependent module source and include directories

里定义了readline的位置,但是我们仍无法加载readline

 

我和同事查了很多的文档和资料,最后我们发现有文档说,我们需要修改某个配置文件,值得python的模块可以正常调用。

python安装目录下,有个名为Module的目录,里边有个Setup.dist的配置文件,基本需要加载的模块都可以在里边找到。

 

Vi这个文件,我们找到readline,会发现有简单的说明,并指明如果要定义一个全新的位置,我们可以通过参数修改

# GNU readline.  Unlike
previous Python incarnations, GNU readline is

# now incorporated in an optional module, configured in the Setup
file

# instead of by a configure script switch.  You
may have to insert a

# -L option pointing to the directory where libreadline.* lives,

# and you may have to change -ltermcap to -ltermlib or perhaps
remove

# it, depending on your system — see the GNU readline instructions.

# It’s okay for this to be a shared library, too.

 

#readline readline.c -lreadline –ltermcap

 

改为:(去掉注释符,指定它的includelib的位置

 

readline readline.c -I/proj/application/tools/readline-5.2-sol/include
-L/proj/application/tools/readline-5.2-sol/lib -R/proj/application/tools/readline-5.2-s

ol/lib -lreadline –ltermcap

 

修改后,我们再次从新编译。

安装python我们需要sqlite模块,所以我事先安装好了,临时设置一下环境变量

export PATH=/proj/application/tools/sqlite-3.7.2-sol/bin:$PATH

export LD_LIBRARY_PATH=/proj/application/tools/sqlite-3.7.2-sol/lib:$LD_LIBRARY_PATH

 

再次编译时,我没有像以前一样去改pythonsetup.py文件,我们做过多次的测试,只要指定好环境变量,应该很顺利。

./configure –prefix=/proj/application/tools/python-2.7.2/ –enable-shared

Make

Makeinstall

 

没有任何报错,readline模块也加载正常。

版权所有,禁止转载. 如需转载,请先征得博主的同意,并且表明文章出处,否则按侵权处理.

    分享到:

Leave a Reply

Your email address will not be published. Required fields are marked *