centos 安裝setuptools、mysqldb
阿新 • • 發佈:2019-01-25
請選擇合適的方式,下載python2.7.3安裝包
http://www.python.org/ftp/python/2.7.3/Python-2.7.3.tar.bz2 安裝前當然需要編譯器gcc- yum install gcc
為了安裝setuptools,要需要安裝zlib
- yum install zlib zlib-dev
解壓安裝python2.7
- tar jxvf Python-2.7.3.tar.bz2
- cd Python-2.7.3
- ./configure
- make && make instal
這裡請注意: 上面這一步是正常步驟,但是在centos5.7環境中,當你執行setuptools時可能會有以下報錯:
-
Traceback (most recent call last):
- File "<string>", line 1, in <module>
- zipimport.ZipImportError: can't decompress data; zlib not available
解決方法是在上面的configure之後,編輯Modules/Setup檔案 找到下面這句,去掉註釋
- #zlib zlibmodule.c -I$(prefix)/include -L$(exec_prefix)/lib -lz
重新編譯安裝:
- make & make install
把系統自帶的2.4移除(依然會保留2.4版本:/usr/bin/python2.4)
-
rm -f /usr/bin/python
把python執行軟連線連線到2.7
- ln -s /usr/local/bin/python2.7 /usr/bin/python
編輯yum命令,把路徑指明為2.4,因為yum必須基於2.4版本
- sed -ie 's#/usr/bin/python$#/usr/bin/python2.4#g' /usr/bin/yum
對應python2.7.3版本的是 下載後,執行
- sh setuptools-0.6c11-py2.7.egg
出現以下資訊表示成功
- 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
- yum install MySQL-devel MySQL-client MySQL-shared-compat MySQL-shared
下載好mysqldb
- 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老是報錯:
- 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都裝上了。 只好從系統查詢該檔案,再建立軟連線
- 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
建立軟連線
- ln -s /usr/lib64/libmysqlclient.so.16.0.0 /usr/lib/libmysqlclient.so
再次install
- python setup.py install
如果這個步驟沒有報錯,表示基本完成 進入python命令列模式
- python
再import MySQLdb,注意大小寫呢
- 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
完成!