環境:python
CentOS 6.4linux
前言:CentOS 6.4系統自帶的有python2.6.6版本c++
1、下載文件web
python官網:https://www.python.org/downloads/ oracle
版本:python-2.7.3app
setuptools官網:https://pypi.python.org/pypi/setuptools#downloads python2.7
版本:setuptools-0.6c11webapp
pip官網:https://pypi.python.org/pypi/pip#downloads函數
版本:pip-1.5.6.tar.gz工具
pymongo官網:https://pypi.python.org/pypi/pymongo#downloads
版本:pymongo-2.7.2.tar.gz
xlrd官網:https://pypi.python.org/pypi/xlrd
版本:xlrd-0.9.3.tar.gz
xlwt官網:https://pypi.python.org/pypi/xlwt
版本:xlwt-0.7.5.tar.gz
xlutils官網:https://pypi.python.org/pypi/xlutils
版本:xlutils-1.7.1.tar.gz
cx_Oracle官網:https://pypi.python.org/pypi/cx_Oracle/5.1.3
版本:cx_Oracle-5.1.3.tar.gz
2、安裝Python2.7.3
一、查看系統自帶的python版本
[root@vm4 webapps]# python -V
Python 2.6.6
二、解壓安裝python
[root@vm4 logs]#cd /usr
[root@vm4 usr]# mkdir -p python
[root@vm4 usr]# cd python/
將下載的文件Python-2.7.3.tgz上傳至python文件夾內
[root@vm4 python]#tar -zvxf Python-2.7.3.tgz
[root@vm4 python]# cd Python-2.7.3
[root@vm4 Python-2.7.3]# ./configure --prefix=/usr/local/python2.7.3
[root@vm4 Python-2.7.3]# make
[root@vm4 Python-2.7.3]# make install
三、安裝成功後目錄
[root@vm4 Python-2.7.3]# ll /usr/local/python2.7.3/
總計 16
drwxr-xr-x 2 root root 4096 03-21 10:12 bin
drwxr-xr-x 3 root root 4096 03-21 10:07 include
drwxr-xr-x 4 root root 4096 03-21 10:07 lib
drwxr-xr-x 3 root root 4096 03-21 10:07 share
四、創建軟件連接
[root@vm4 Python-2.7.3]# ln -s /usr/local/Python-2.7.3/bin/python /usr/bin/python2.7.3
[root@vm4 Python-2.7.3]# python2.7.3
Python 2.7.3 (default, Mar 21 2013, 10:06:48)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-50)] on linux2
注意:
若是執行操做[root@vm4 Python-2.7.3]#ln -s /usr/python/Python-2.7.3/bin/python /usr/bin/python
修改老版本的ln指向後,可能會影響yum的使用,yum兼容Python2.6版本。
操做到這裏,python就安裝成功了.
3、安裝python-devel
[root@vm4 python]# yum install python-devel
Installed:
python-devel.x86_64 0:2.6.6-52.el6
Dependency Updated:
python.x86_64 0:2.6.6-52.el6 python-
libs.x86_64 0:2.6.6-52.el6
Complete!
注意:因爲python-devel問題,建議python使用默認的版本2.6.6。上面只是介紹如何安裝python。
4、安裝setuptools
注意:安裝成功python以後,才能安裝setuptools
一、將下載的文件上傳setuptools-0.6c11.tar.gz至python文件夾內
二、解壓安裝setuptools-0.6c11
[root@vm4 python]# tar -zxvf setuptools-0.6c11.tar.gz
[root@vm4 setuptools-0.6c11]# cd setuptools-0.6c11
[root@vm4 setuptools-0.6c11]# Python-2.7.3 setup.py install
返回以下表示安裝成功:
Installed /usr/python/Python-2.7.3/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processing dependencies for setuptools==0.6c11
Finished processing dependencies for setuptools==0.6c11
三、創建軟連接
[root@vm4 setuptools-0.6c11]#ln -s /usr/python/Python-2.7.3/bin/easy_install /usr/bin/easy_install-2.7.3
四、測試
[root@vm4 setuptools-0.6c11]#easy_install2.7.3 numpy
5、安裝pip
一、將下載的文件上傳pip-1.5.6.tar.gz至python文件夾內
二、解壓安裝pip-1.5.6
[root@vm4 python]#tar -zxvf pip-1.5.6
[root@vm4 python]# cd pip-1.5.6
[root@vm4 pip-1.5.6]# Python-2.7.3 setup.py install
返回以下表示安裝成功:
Installed /usr/python/Python-2.7.3/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg
Processing dependencies for pip==1.5.6
Finished processing dependencies for pip==1.5.6
三、創建軟連接
[root@vm4 pip-1.5.6]#ln -s /usr/python/Python-2.7.3/bin/pip /usr/bin/pip-2.7.3
四、測試
[root@vm4 pip-1.5.6]#pip-2.7.3 install nose
5、安裝pymongo
python操做mongo時,須要安裝此插件
第一種方法:
[root@vm4 python]# pip-2.7.3 install pymongo
第二種方法:
一、下載文件pymongo-2.7.2.tar.gz並上傳至python
二、解壓安裝
[root@vm4 python]#tar -zxvf pymongo-2.7.2.tar.gz
[root@vm4 python]# cd pymongo-2.7.2
[root@vm4 pymongo-2.7.2]# Python-2.7.3 setup.py install
返回以下表示安裝成功:
Installed /usr/python/Python-2.7.3/lib/python2.7/site-packages/pymongo-2.7.2-py2.7-linux-
x86_64.egg
Processing dependencies for pymongo==2.7.2
Finished processing dependencies for pymongo==2.7.2
6、安裝xlrd、xlwt、xlutils
xlrd:是python從excel讀數據的第三方控件;
xlwt:是python從excel寫數據的第三方控件;
xlutils:是python使用xlrd、xlwt的工具箱。若安裝不成功,可能緣由是須要安裝setuptools。
第一種方法:
[root@vm4 python]# pip-2.7.3 install xlrd
[root@vm4 python]# pip-2.7.3 install xlwt
[root@vm4 python]# pip-2.7.3 install xlutils
第二種方法:
一、下載文件xlrd-0.9.3.tar.gz,xlwt-0.7.5.tar.gz,xlutils-1.7.1.tar.gz並上傳至python
二、解壓安裝xlrd
[root@vm4 python]# tar -zxvf xlrd-0.9.3.tar.gz
[root@vm4 python]# cd xlrd-0.9.3
[root@vm4 xlrd-0.9.3]# Python-2.7.3 setup.py install
三、解壓安裝xlwt
[root@vm4 python]# tar -zxvf xlwt-0.7.5.tar.gz
[root@vm4 python]# cd xlwt-0.7.5
[root@vm4 xlwt-0.7.5]# Python-2.7.3 setup.py install
四、解壓安裝xlutils
[root@vm4 python]# tar -zxvf xlutils-1.7.1.tar.gz
[root@vm4 python]# cd xlutils-1.7.1
[root@vm4 xlutils-1.7.1]# Python-2.7.3 setup.py install
7、安裝cx_Oracle
python操做oracle時, 須要安裝此插件。安裝完oracle客戶端以後才能安裝cx_Oracle.
[root@vm4 python]# pip-2.7.3 install cx_Oracle
Downloading/unpacking cx-Oracle
Downloading cx_Oracle-5.1.3.tar.gz (104kB): 104kB downloaded
Running setup.py (path:/tmp/pip_build_root/cx-Oracle/setup.py) egg_info for package cx-Oracle
Installing collected packages: cx-Oracle
Running setup.py install for cx-Oracle
building 'cx_Oracle' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/opt/oracle/instantclient_11_2/sdk/include -I/usr/include/python2.6 -c cx_Oracle.c -o build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -DBUILD_VERSION=5.1.3
gcc -pthread -shared build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -L/opt/oracle/instantclient_11_2/ -L/usr/lib64 -lclntsh -lpython2.6 -o build/lib.linux-x86_64-2.6-11g/cx_Oracle.so
Successfully installed cx-Oracle
Cleaning up...
安裝成功後創建軟連接
[root@localhost instantclient_11_2]# ln -s libclntsh.so.11.1 libclntsh.so
(以上安裝成功後的包在/usr/lib64/python2.6/site-packages)
8、安裝過程當中遇到的問題
一、在安裝了Python2.7.3以後,執行
再使用yum時,報錯:It's possible that the above module doesn't match the current version of Python
分析緣由:CentOS 6.4系統默認Python版本是2.6.6,須要升級到Python 2.7.3,因爲yum包管理是用python寫的,僅僅單獨升級python會致使yum沒法使用.
出現這種狀況的主要緣由在於新安裝的Python沒有YUM服務所依賴的Packages.
解決方法:查找yum文件,並編輯此py文件
[root@Centos ~]# which yum
/usr/bin/yum
[root@Centos ~]# vi /usr/bin/yum
將
#!/usr/bin/python
改成:
#!/usr/bin/python2.6.6
而後保存OK.
另外,還有一種可能也一樣會致使沒法使用yum,就是/usr/bin下的python、python二、python2.4三個文件必定不能改變。理論上說yum只是基於python語言,跟版本並無關係,可是升級過程當中確實會出現問題
二、在python運行過程當中出現以下錯誤:
python錯誤:ImportError: No module named setuptools
這句錯誤提示的表面意思是:沒有setuptools的模塊,說明python缺乏這個模塊,那咱們只要安裝這個
模塊便可解決此問題
解決方法:安裝setuptools
三、執行python腳本時,報錯
[root@vm4 statistics]# Python-2.7.3 generator.py
Traceback (most recent call last):
File "generator.py", line 3, in <module>
import cx_Oracle
ImportError: No module named cx_Oracle
解決方法:安裝cx_Oracle
四、[root@localhost site-packages]# pip install cx_Oracle報錯:
Downloading/unpacking cx-Oracle
Downloading cx_Oracle-5.1.3.tar.gz (104kB): 104kB downloaded
Running setup.py (path:/tmp/pip_build_root/cx-Oracle/setup.py) egg_info for package cx-Oracle
Installing collected packages: cx-Oracle
Running setup.py install for cx-Oracle
building 'cx_Oracle' extension
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/opt/oracle/instantclient_11_2/sdk/include -I/usr/include/python2.6 -c cx_Oracle.c -o build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -DBUILD_VERSION=5.1.3
gcc -pthread -shared build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -L/opt/oracle/instantclient_11_2/ -L/usr/lib64 -lclntsh -lpython2.6 -o build/lib.linux-x86_64-2.6-11g/cx_Oracle.so
/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
Complete output from command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_root/cx-Oracle/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ONXD3d-record/install-record.txt --single-version-externally-managed --compile:
running install
running build
running build_ext
building 'cx_Oracle' extension
creating build
creating build/temp.linux-x86_64-2.6-11g
gcc -pthread -fno-strict-aliasing -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -DNDEBUG -O2 -g -pipe -Wall -Wp,-D_FORTIFY_SOURCE=2 -fexceptions -fstack-protector --param=ssp-buffer-size=4 -m64 -mtune=generic -D_GNU_SOURCE -fPIC -fwrapv -fPIC -I/opt/oracle/instantclient_11_2/sdk/include -I/usr/include/python2.6 -c cx_Oracle.c -o build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -DBUILD_VERSION=5.1.3
creating build/lib.linux-x86_64-2.6-11g
gcc -pthread -shared build/temp.linux-x86_64-2.6-11g/cx_Oracle.o -L/opt/oracle/instantclient_11_2/ -L/usr/lib64 -lclntsh -lpython2.6 -o build/lib.linux-x86_64-2.6-11g/cx_Oracle.so
/usr/bin/ld: cannot find -lclntsh
collect2: ld returned 1 exit status
error: command 'gcc' failed with exit status 1
----------------------------------------
Cleaning up...
Command /usr/bin/python -c "import setuptools, tokenize;__file__='/tmp/pip_build_root/cx-Oracle/setup.py';exec(compile(getattr(tokenize, 'open', open)(__file__).read().replace('\r\n', '\n'), __file__, 'exec'))" install --record /tmp/pip-ONXD3d-record/install-record.txt --single-version-externally-managed --compile failed with error code 1 in /tmp/pip_build_root/cx-Oracle
Storing debug log for failure in /root/.pip/pip.log
解決方法:[root@localhost instantclient_11_2]# ln -s libclntsh.so.11.1 libclntsh.so
五、執行python腳本時,報./startup.sh: Permission denied
解決方法:chmod u+x *.sh
六、[root@localhost statistics]# ./generator.py
Traceback (most recent call last):
File "./generator.py", line 3, in <module>
import cx_Oracle
ImportError: libaio.so.1: cannot open shared object file: No such file or directory
解決方法:[root@localhost statistics]# yum install libaio
9、python和python-dev以及boost.python的關係
不少時候,不少人搞不清楚,python和python-dev以及boost.python的關係。python是執行環境 ,若是你要c/c++中調用python,或者python調用c或者c++,則須要python-dev。boost.python僅僅是對 python-dev進行了封裝,不少函數,兩邊都具備一樣的功能,也就是說用python-dev中的也能夠如run1 函數,用boost.python中的也行如run2,boost.python的接口封裝得更爲友好而已如異常處理。