Centos 5.x 升级 python2.7,安装setuptools、mysqldb 完整记录

时间:2025-01-14 14:04:20

最近由于有个工作任务需要搭个虚拟机环境,但是环境是搭建在内网,无法直接联网,很多软件都不能直接yum安装,

安装过程实在十分不顺利,在此留个记录给有需要的朋友。

环境是 CentOS 5.7 x64

请选择合适的方式,下载python2.7.3安装包

http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2

安装前当然需要编译器gcc
  1. yum install gcc
yum install gcc
为了安装setuptools,要需要安装zlib
  1. yum install zlib zlib-dev
yum install zlib zlib-dev
解压安装python2.7
  1. tar jxvf Python-2.7.3.tar.bz2
  2. cd Python-2.7.3
  3. ./configure
  4. make && make instal
tar jxvf Python-2.7.3.tar.bz2
cd Python-2.7.3
./configure
make && make instal
这里请注意:
上面这一步是正常步骤,但是在centos5.7环境中,当你执行setuptools时可能会有以下报错:
  1. Traceback (most recent call last):
  2. File "<string>", line 1, in <module>
  3. zipimport.ZipImportError: can't decompress data; zlib not available
Traceback (most recent call last):
File "<string>", line 1, in <module>
zipimport.ZipImportError: can't decompress data; zlib not available
解决方法是在上面的configure之后,编辑Modules/Setup文件
找到下面这句,去掉注释
  1. #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
#zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz 
重新编译安装:
  1. make & make install
make & make install 
把系统自带的2.4移除(依然会保留2.4版本:/usr/bin/python2.4)
  1. rm -f /usr/bin/python
rm -f /usr/bin/python

把python执行软连接连接到2.7

  1. ln -s /usr/local/bin/python2.7 /usr/bin/python
ln -s /usr/local/bin/python2.7 /usr/bin/python

编辑yum命令,把路径指明为2.4,因为yum必须基于2.4版本

  1. sed -ie 's#/usr/bin/python$#/usr/bin/python2.4#g' /usr/bin/yum
sed -ie 's#/usr/bin/python$#/usr/bin/python2.4#g' /usr/bin/yum 
对应python2.7.3版本的是
下载后,执行
  1. sh setuptools-0.6c11-py2.7.egg
sh setuptools-0.6c11-py2.7.egg
出现以下信息表示成功
  1. Processing setuptools-0.6c11-py2.7.egg
  2. Copying setuptools-0.6c11-py2.7.egg to /usr/local/lib/python2.7/site-packages
  3. Adding setuptools 0.6c11 to easy-install.pth file
  4. Installing easy_install script to /usr/local/bin
  5. Installing easy_install-2.7 script to /usr/local/bin
  6. Installed /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
  7. Processing dependencies for setuptools==0.6c11
  8. Finished processing dependencies for setuptools==0.6c11
Processing setuptools-0.6c11-py2.7.egg
Copying setuptools-0.6c11-py2.7.egg to /usr/local/lib/python2.7/site-packages
Adding setuptools 0.6c11 to easy-install.pth file
Installing easy_install script to /usr/local/bin
Installing easy_install-2.7 script to /usr/local/bin Installed /usr/local/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11
安装mysql相关包
  1. yum install MySQL-devel MySQL-client MySQL-shared-compat MySQL-shared
yum install MySQL-devel MySQL-client MySQL-shared-compat MySQL-shared

下载好mysqldb

  1. tar xvzf MySQL-python-1.2.3.tar.gz
  2. cd MySQL-python-1.2.3
  3. vim site.cfg
  4. 把上面的mysql_config路径注释删除以及改成实际路径,如果不知道mysql_config在哪里,运行命令:whereis mysql_config
  5. python setup.py install
tar xvzf MySQL-python-1.2.3.tar.gz
cd MySQL-python-1.2.3
vim site.cfg
把上面的mysql_config路径注释删除以及改成实际路径,如果不知道mysql_config在哪里,运行命令:whereis mysql_config
python setup.py install
安装mysqldb老是报错:
  1. running build
  2. running build_py
  3. copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
  4. running build_ext
  5. building '_mysql' extension
  6. gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -m64 -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
  7. 在包含自 _mysql.c:36 的文件中:
  8. /usr/include/mysql/my_config.h:422:1: 警告:“HAVE_WCSCOLL”重定义
  9. 在包含自 /usr/local/include/python2.7/Python.h:8 的文件中,
  10. 从 pymemcompat.h:10,
  11. 从 _mysql.c:29:
  12. /usr/local/include/python2.7/pyconfig.h:890:1: 警告:这是先前定义的位置
  13. gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
  14. /usr/bin/ld: cannot find -lmysqlclient
  15. collect2: ld 返回 1
running build
running build_py
copying MySQLdb/release.py -> build/lib.linux-x86_64-2.7/MySQLdb
running build_ext
building '_mysql' extension
gcc -pthread -fno-strict-aliasing -g -O2 -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -fPIC -Dversion_info=(1,2,3,'final',0) -D__version__=1.2.3 -I/usr/include/mysql -I/usr/local/include/python2.7 -c _mysql.c -o build/temp.linux-x86_64-2.7/_mysql.o -g -pipe -m64 -fPIC -g -static-libgcc -fno-omit-frame-pointer -fno-strict-aliasing -DMY_PTHREAD_FASTMUTEX=1
在包含自 _mysql.c:36 的文件中:
/usr/include/mysql/my_config.h:422:1: 警告:“HAVE_WCSCOLL”重定义
在包含自 /usr/local/include/python2.7/Python.h:8 的文件中,
从 pymemcompat.h:10,
从 _mysql.c:29:
/usr/local/include/python2.7/pyconfig.h:890:1: 警告:这是先前定义的位置
gcc -pthread -shared build/temp.linux-x86_64-2.7/_mysql.o -L/usr/lib64 -lmysqlclient -lpthread -lm -lrt -ldl -o build/lib.linux-x86_64-2.7/_mysql.so
/usr/bin/ld: cannot find -lmysqlclient
collect2: ld 返回 1
说找不到 mysqlclient ,真心不可能,我把mysql-devel 、 mysql-client、mysql-shared都装上了。
只好从系统查找该文件,再建立软连接
  1. find / -name 'libmysqlclient.so.*'
  2. /usr/lib64/libmysqlclient.so.14
  3. /usr/lib64/libmysqlclient.so.12
  4. /usr/lib64/libmysqlclient.so.14.0.0
  5. /usr/lib64/libmysqlclient.so.12.0.0
  6. /usr/lib64/libmysqlclient.so.16
  7. /usr/lib64/libmysqlclient.so.15
  8. /usr/lib64/libmysqlclient.so.15.0.0
  9. /usr/lib64/libmysqlclient.so.16.0.0
find / -name 'libmysqlclient.so.*'
/usr/lib64/libmysqlclient.so.14
/usr/lib64/libmysqlclient.so.12
/usr/lib64/libmysqlclient.so.14.0.0
/usr/lib64/libmysqlclient.so.12.0.0
/usr/lib64/libmysqlclient.so.16
/usr/lib64/libmysqlclient.so.15
/usr/lib64/libmysqlclient.so.15.0.0
/usr/lib64/libmysqlclient.so.16.0.0
建立软连接
  1. ln -s /usr/lib64/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
ln -s /usr/lib64/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
再次install
  1. python setup.py install
python setup.py install
如果这个步骤没有报错,表示基本完成
进入python命令行模式
  1. python
python

再import MySQLdb,注意大小写呢

  1. Python 2.7.3 (default, Dec 10 2012, 14:33:49)
  2. [GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >>> import MySQLdb
  5. /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.pyc, but /home/soft/MySQL-python-1.2.3 is being added to sys.path
Python 2.7.3 (default, Dec 10 2012, 14:33:49)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-51)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
/usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /usr/local/lib/python2.7/site-packages/MySQL_python-1.2.3-py2.7-linux-x86_64.egg/_mysql.pyc, but /home/soft/MySQL-python-1.2.3 is being added to sys.path
完成!