Linux搭建python环境详解
一、下载文件
python官网:https://www.python.org/downloads/
版本:python-2.7.3下载地址:https://www.nhooo.com/softs/2293.html
setuptools官网:https://pypi.python.org/pypi/setuptools#downloads
版本:setuptools-0.6c11
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
二、安装Python2.7.3
1、查看系统自带的python版本
[root@vm4webapps]#python-V
Python2.6.6
2、解压安装python
[root@vm4logs]#cd/usr [root@vm4usr]#mkdir-ppython [root@vm4usr]#cdpython/
将下载的文件Python-2.7.3.tgz上传至python文件夹内
[root@vm4python]#tar-zvxfPython-2.7.3.tgz [root@vm4python]#cdPython-2.7.3 [root@vm4Python-2.7.3]#./configure--prefix=/usr/local/python2.7.3 [root@vm4Python-2.7.3]#make [root@vm4Python-2.7.3]#makeinstall
3、安装成功后目录
[root@vm4Python-2.7.3]#ll/usr/local/python2.7.3/
总计16
drwxr-xr-x2rootroot409603-2110:12bin drwxr-xr-x3rootroot409603-2110:07include drwxr-xr-x4rootroot409603-2110:07lib drwxr-xr-x3rootroot409603-2110:07share
4、建立软件链接
[root@vm4Python-2.7.3]#ln-s/usr/local/Python-2.7.3/bin/python/usr/bin/python2.7.3 [root@vm4Python-2.7.3]#python2.7.3 Python2.7.3(default,Mar212013,10:06:48) [GCC4.1.220080704(RedHat4.1.2-50)]onlinux2
注意:
如果执行操作[root@vm4Python-2.7.3]#ln-s/usr/python/Python-2.7.3/bin/python/usr/bin/python
修改老版本的ln指向后,可能会影响yum的使用,yum兼容Python2.6版本。
操作到这里,python就安装成功了.
三、安装python-devel
[root@vm4python]#yuminstallpython-devel
Installed:
python-devel.x86_640:2.6.6-52.el6
DependencyUpdated:
python.x86_640:2.6.6-52.el6python-
libs.x86_640:2.6.6-52.el6
Complete!
注意:由于python-devel问题,建议python使用默认的版本2.6.6。上面只是介绍如何安装python。
四、安装setuptools
注意:安装成功python之后,才能安装setuptools
1、将下载的文件上传setuptools-0.6c11.tar.gz至python文件夹内
2、解压安装setuptools-0.6c11
[root@vm4python]#tar-zxvfsetuptools-0.6c11.tar.gz [root@vm4setuptools-0.6c11]#cdsetuptools-0.6c11 [root@vm4setuptools-0.6c11]#Python-2.7.3setup.pyinstall
返回如下表示安装成功:
Installed/usr/python/Python-2.7.3/lib/python2.7/site-packages/setuptools-0.6c11-py2.7.egg
Processingdependenciesforsetuptools==0.6c11
Finishedprocessingdependenciesforsetuptools==0.6c11
3、建立软链接
[root@vm4setuptools-0.6c11]#ln-s/usr/python/Python-2.7.3/bin/easy_install/usr/bin/easy_install-2.7.3
4、测试
[root@vm4setuptools-0.6c11]#easy_install2.7.3numpy
五、安装pip
1、将下载的文件上传pip-1.5.6.tar.gz至python文件夹内
2、解压安装pip-1.5.6
[root@vm4python]#tar-zxvfpip-1.5.6 [root@vm4python]#cdpip-1.5.6 [root@vm4pip-1.5.6]#Python-2.7.3setup.pyinstall
返回如下表示安装成功:
Installed/usr/python/Python-2.7.3/lib/python2.7/site-packages/pip-1.5.6-py2.7.egg
Processingdependenciesforpip==1.5.6
Finishedprocessingdependenciesforpip==1.5.6
3、建立软链接
[root@vm4pip-1.5.6]#ln-s/usr/python/Python-2.7.3/bin/pip/usr/bin/pip-2.7.3
4、测试
[root@vm4pip-1.5.6]#pip-2.7.3installnose
五、安装pymongo
python操作mongo时,需要安装此插件
第一种方法:
[root@vm4python]#pip-2.7.3installpymongo
第二种方法:
1、下载文件pymongo-2.7.2.tar.gz并上传至python
2、解压安装
[root@vm4python]#tar-zxvfpymongo-2.7.2.tar.gz [root@vm4python]#cdpymongo-2.7.2 [root@vm4pymongo-2.7.2]#Python-2.7.3setup.pyinstall
返回如下表示安装成功:
Installed/usr/python/Python-2.7.3/lib/python2.7/site-packages/pymongo-2.7.2-py2.7-linux-x86_64.egg
Processingdependenciesforpymongo==2.7.2
Finishedprocessingdependenciesforpymongo==2.7.2
六、安装xlrd、xlwt、xlutils
xlrd:是python从excel读数据的第三方控件;
xlwt:是python从excel写数据的第三方控件;
xlutils:是python使用xlrd、xlwt的工具箱。若安装不成功,可能原因是需要安装setuptools。
第一种方法:
[root@vm4python]#pip-2.7.3installxlrd [root@vm4python]#pip-2.7.3installxlwt [root@vm4python]#pip-2.7.3installxlutils
第二种方法:
1、下载文件xlrd-0.9.3.tar.gz,xlwt-0.7.5.tar.gz,xlutils-1.7.1.tar.gz并上传至python
2、解压安装xlrd
[root@vm4python]#tar-zxvfxlrd-0.9.3.tar.gz [root@vm4python]#cdxlrd-0.9.3 [root@vm4xlrd-0.9.3]#Python-2.7.3setup.pyinstall
3、解压安装xlwt
[root@vm4python]#tar-zxvfxlwt-0.7.5.tar.gz [root@vm4python]#cdxlwt-0.7.5 [root@vm4xlwt-0.7.5]#Python-2.7.3setup.pyinstall
4、解压安装xlutils
[root@vm4python]#tar-zxvfxlutils-1.7.1.tar.gz [root@vm4python]#cdxlutils-1.7.1 [root@vm4xlutils-1.7.1]#Python-2.7.3setup.pyinstall
七、安装cx_Oracle
python操作oracle时,需要安装此插件。安装完oracle客户端之后才能安装cx_Oracle.
[root@vm4python]#pip-2.7.3installcx_Oracle
Downloading/unpackingcx-Oracle
Downloadingcx_Oracle-5.1.3.tar.gz(104kB):104kBdownloaded
Runningsetup.py(path:/tmp/pip_build_root/cx-Oracle/setup.py)egg_infoforpackagecx-Oracle
Installingcollectedpackages:cx-Oracle
Runningsetup.pyinstallforcx-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-ccx_Oracle.c-obuild/temp.linux-x86_64-2.6-11g/cx_Oracle.o-DBUILD_VERSION=5.1.3
gcc-pthread-sharedbuild/temp.linux-x86_64-2.6-11g/cx_Oracle.o-L/opt/oracle/instantclient_11_2/-L/usr/lib64-lclntsh-lpython2.6-obuild/lib.linux-x86_64-2.6-11g/cx_Oracle.so
Successfullyinstalledcx-Oracle
Cleaningup...
安装成功后建立软链接
[root@localhostinstantclient_11_2]#ln-slibclntsh.so.11.1libclntsh.so
(以上安装成功后的包在/usr/lib64/python2.6/site-packages)
八、安装过程中遇到的问题
1、在安装了Python2.7.3之后,执行
[root@vm4Python-2.7.3]#mv/usr/bin/python/usr/bin/python2.6.6 [root@vm4Python-2.7.3]#ln-s/usr/python/Python-2.7.3/bin/python/usr/bin/python
再使用yum时,报错:It'spossiblethattheabovemoduledoesn'tmatchthecurrentversionofPython
分析原因:CentOS6.4系统默认Python版本是2.6.6,需要升级到Python2.7.3,由于yum包管理是用python写的,仅仅单独升级python会导致yum无法使用.
出现这种情况的主要原因在于新安装的Python没有YUM服务所依赖的Packages.
解决方法:查找yum文件,并编辑此py文件
[root@Centos~]#whichyum /usr/bin/yum [root@Centos~]#vi/usr/bin/yum
将
#!/usr/bin/python
改为:
#!/usr/bin/python2.6.6
然后保存OK.
另外,还有一种可能也同样会导致无法使用yum,就是/usr/bin下的python、python2、python2.4三个文件一定不能改变。理论上说yum只是基于python语言,跟版本并没有关系,但是升级过程中确实会出现问题
2、在python运行过程中出现如下错误:
python错误:ImportError:Nomodulenamedsetuptools
这句错误提示的表面意思是:没有setuptools的模块,说明python缺少这个模块,那我们只要安装这个模块即可解决此问题
解决方法:安装setuptools
3、执行python脚本时,报错
[root@vm4statistics]#Python-2.7.3generator.py
Traceback(mostrecentcalllast): File"generator.py",line3,in<module>
importcx_Oracle
ImportError:Nomodulenamedcx_Oracle
解决方法:安装cx_Oracle
4、[root@localhostsite-packages]#pipinstallcx_Oracle报错:
Downloading/unpackingcx-Oracle
Downloadingcx_Oracle-5.1.3.tar.gz(104kB):104kBdownloaded
Runningsetup.py(path:/tmp/pip_build_root/cx-Oracle/setup.py)egg_infoforpackagecx-Oracle
Installingcollectedpackages:cx-Oracle
Runningsetup.pyinstallforcx-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-ccx_Oracle.c-obuild/temp.linux-x86_64-2.6-11g/cx_Oracle.o-DBUILD_VERSION=5.1.3
gcc-pthread-sharedbuild/temp.linux-x86_64-2.6-11g/cx_Oracle.o-L/opt/oracle/instantclient_11_2/-L/usr/lib64-lclntsh-lpython2.6-obuild/lib.linux-x86_64-2.6-11g/cx_Oracle.so
/usr/bin/ld:cannotfind-lclntsh
collect2:ldreturned1exitstatus
error:command'gcc'failedwithexitstatus1
Completeoutputfromcommand/usr/bin/python-c"importsetuptools,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:
runninginstall
runningbuild
runningbuild_ext
building'cx_Oracle'extension
creatingbuild
creatingbuild/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-ccx_Oracle.c-obuild/temp.linux-x86_64-2.6-11g/cx_Oracle.o-DBUILD_VERSION=5.1.3
creatingbuild/lib.linux-x86_64-2.6-11g
gcc-pthread-sharedbuild/temp.linux-x86_64-2.6-11g/cx_Oracle.o-L/opt/oracle/instantclient_11_2/-L/usr/lib64-lclntsh-lpython2.6-obuild/lib.linux-x86_64-2.6-11g/cx_Oracle.so
/usr/bin/ld:cannotfind-lclntsh
collect2:ldreturned1exitstatus
error:command'gcc'failedwithexitstatus1
----------------------------------------
Cleaningup...
Command/usr/bin/python-c"importsetuptools,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--compilefailedwitherrorcode1in/tmp/pip_build_root/cx-Oracle
Storingdebuglogforfailurein/root/.pip/pip.log
解决方法:[root@localhostinstantclient_11_2]#ln-slibclntsh.so.11.1libclntsh.so
5、执行python脚本时,报./startup.sh:Permissiondenied
解决方法:chmodu+x*.sh
6、
[root@localhoststatistics]#./generator.py
Traceback(mostrecentcalllast):File"./generator.py",line3,in<module>
importcx_Oracle
ImportError:libaio.so.1:cannotopensharedobjectfile:Nosuchfileordirectory
解决方法:[root@localhoststatistics]#yuminstalllibaio
九、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的接口封装得更为友好而已如异常处理。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持毛票票。