一、下載文件
python官網(wǎng):https://www.python.org/downloads/
版本:python-2.7.3
setuptools官網(wǎng):https://pypi.python.org/pypi/setuptools#downloads
版本:setuptools-0.6c11
pip官網(wǎng):https://pypi.python.org/pypi/pip#downloads
版本:pip-1.5.6.tar.gz
pymongo官網(wǎng):https://pypi.python.org/pypi/pymongo#downloads
版本:pymongo-2.7.2.tar.gz
xlrd官網(wǎng):https://pypi.python.org/pypi/xlrd
版本:xlrd-0.9.3.tar.gz
xlwt官網(wǎng):https://pypi.python.org/pypi/xlwt
版本:xlwt-0.7.5.tar.gz
xlutils官網(wǎng):https://pypi.python.org/pypi/xlutils
版本:xlutils-1.7.1.tar.gz
cx_Oracle官網(wǎng):https://pypi.python.org/pypi/cx_Oracle/5.1.3
版本:cx_Oracle-5.1.3.tar.gz
二、安裝Python2.7.3
1、查看系統(tǒng)自帶的python版本
1
|
[root@vm4 webapps] # python -V |
Python 2.6.6
2、解壓安裝python
1
2
3
4
5
|
[root@vm4 logs] #cd /usr [root@vm4 usr] # mkdir -p python [root@vm4 usr] # cd python/ |
將下載的文件Python-2.7.3.tgz上傳至python文件夾內(nèi)
1
2
3
4
5
6
7
8
9
|
[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 |
3、安裝成功后目錄
1
|
[root@vm4 Python-2.7.3] # ll /usr/local/python2.7.3/ |
總計(jì) 16
1
2
3
4
5
6
7
|
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 |
4、建立軟件鏈接
1
2
3
4
5
6
7
|
[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 |
注意:
如果執(zhí)行操作[root@vm4 Python-2.7.3]#ln -s /usr/python/Python-2.7.3/bin/python /usr/bin/python
修改老版本的ln指向后,可能會(huì)影響yum的使用,yum兼容Python2.6版本。
操作到這里,python就安裝成功了.
三、安裝python-devel
1
|
[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問(wèn)題,建議python使用默認(rèn)的版本2.6.6。上面只是介紹如何安裝python。
四、安裝setuptools
注意:安裝成功python之后,才能安裝setuptools
1、將下載的文件上傳setuptools-0.6c11.tar.gz至python文件夾內(nèi)
2、解壓安裝setuptools-0.6c11
1
2
3
|
[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
3、建立軟鏈接
1
|
[root@vm4 setuptools-0.6c11] #ln -s /usr/python/Python-2.7.3/bin/easy_install /usr/bin/easy_install-2.7.3 |
4、測(cè)試
1
|
[root@vm4 setuptools-0.6c11] #easy_install2.7.3 numpy |
五、安裝pip
1、將下載的文件上傳pip-1.5.6.tar.gz至python文件夾內(nèi)
2、解壓安裝pip-1.5.6
1
2
3
|
[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
3、建立軟鏈接
1
|
[root@vm4 pip-1.5.6] #ln -s /usr/python/Python-2.7.3/bin/pip /usr/bin/pip-2.7.3 |
4、測(cè)試
1
|
[root@vm4 pip-1.5.6] #pip-2.7.3 install nose |
五、安裝pymongo
python操作mongo時(shí),需要安裝此插件
第一種方法:
1
|
[root@vm4 python] # pip-2.7.3 install pymongo |
第二種方法:
1、下載文件pymongo-2.7.2.tar.gz并上傳至python
2、解壓安裝
1
2
3
|
[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
六、安裝xlrd、xlwt、xlutils
xlrd:是python從excel讀數(shù)據(jù)的第三方控件;
xlwt:是python從excel寫(xiě)數(shù)據(jù)的第三方控件;
xlutils:是python使用xlrd、xlwt的工具箱。若安裝不成功,可能原因是需要安裝setuptools。
第一種方法:
1
2
3
4
5
|
[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 |
第二種方法:
1、下載文件xlrd-0.9.3.tar.gz,xlwt-0.7.5.tar.gz,xlutils-1.7.1.tar.gz并上傳至python
2、解壓安裝xlrd
1
2
3
|
[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 |
3、解壓安裝xlwt
1
2
3
|
[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 |
4、解壓安裝xlutils
1
2
3
|
[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 |
七、安裝cx_Oracle
python操作oracle時(shí), 需要安裝此插件。安裝完oracle客戶端之后才能安裝cx_Oracle.
1
|
[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...
安裝成功后建立軟鏈接
1
|
[root@localhost instantclient_11_2] # ln -s libclntsh.so.11.1 libclntsh.so |
(以上安裝成功后的包在/usr/lib64/python2.6/site-packages)
八、安裝過(guò)程中遇到的問(wèn)題
1、在安裝了Python2.7.3之后,執(zhí)行
1
2
|
[root@vm4 Python-2.7.3] #mv /usr/bin/python /usr/bin/python2.6.6 [root@vm4 Python-2.7.3] #ln -s /usr/python/Python-2.7.3/bin/python /usr/bin/python |
再使用yum時(shí),報(bào)錯(cuò):It's possible that the above module doesn't match the current version of Python
分析原因:CentOS 6.4系統(tǒng)默認(rèn)Python版本是2.6.6,需要升級(jí)到Python 2.7.3,由于yum包管理是用python寫(xiě)的,僅僅單獨(dú)升級(jí)python會(huì)導(dǎo)致yum無(wú)法使用.
出現(xiàn)這種情況的主要原因在于新安裝的Python沒(méi)有YUM服務(wù)所依賴的Packages.
解決方法:查找yum文件,并編輯此py文件
1
2
3
|
[root@Centos ~] # which yum /usr/bin/yum [root@Centos ~] # vi /usr/bin/yum |
將
1
|
#!/usr/bin/python |
改為:
1
|
#!/usr/bin/python2.6.6 |
然后保存OK.
另外,還有一種可能也同樣會(huì)導(dǎo)致無(wú)法使用yum,就是/usr/bin下的python、python2、python2.4三個(gè)文件一定不能改變。理論上說(shuō)yum只是基于python語(yǔ)言,跟版本并沒(méi)有關(guān)系,但是升級(jí)過(guò)程中確實(shí)會(huì)出現(xiàn)問(wèn)題
2、在python運(yùn)行過(guò)程中出現(xiàn)如下錯(cuò)誤:
python錯(cuò)誤:ImportError: No module named setuptools
這句錯(cuò)誤提示的表面意思是:沒(méi)有setuptools的模塊,說(shuō)明python缺少這個(gè)模塊,那我們只要安裝這個(gè) 模塊即可解決此問(wèn)題
解決方法:安裝setuptools
3、執(zhí)行python腳本時(shí),報(bào)錯(cuò)
1
|
[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
4、[root@localhost site-packages]# pip install cx_Oracle報(bào)錯(cuò):
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
5、執(zhí)行python腳本時(shí),報(bào)./startup.sh: Permission denied
解決方法:chmod u+x *.sh
6、
1
|
[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
九、python和python-dev以及boost.python的關(guān)系
很多時(shí)候,很多人搞不清楚,python和python-dev以及boost.python的關(guān)系。python是執(zhí)行環(huán)境 ,如果你要c/c++中調(diào)用python,或者python調(diào)用c或者c++,則需要python-dev。boost.python僅僅是對(duì) python-dev進(jìn)行了封裝,很多函數(shù),兩邊都具有同樣的功能,也就是說(shuō)用python-dev中的也可以如run1 函數(shù),用boost.python中的也行如run2,boost.python的接口封裝得更為友好而已如異常處理。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/yangxia-test/p/4160022.html