Linux搭建python环境

时间:2020-11-29 12:03:47

环境:

CentOS 6.4

 

前言:CentOS 6.4系统自带的有python2.6.6版本

 

一、下载文件

python官网:https://www.python.org/downloads/    

版本:python-2.7.3

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@vm4 webapps]# python -V
Python 2.6.6

 

2、解压安装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

 

3、安装成功后目录

[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

 

4、建立软件链接

[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就安装成功了.

 

三、安装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。

 

四、安装setuptools

 注意:安装成功python之后,才能安装setuptools

1、将下载的文件上传setuptools-0.6c11.tar.gz至python文件夹内

2、解压安装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

3、建立软链接

[root@vm4 setuptools-0.6c11]#ln -s /usr/python/Python-2.7.3/bin/easy_install /usr/bin/easy_install-2.7.3

4、测试

[root@vm4 setuptools-0.6c11]#easy_install2.7.3 numpy

 

五、安装pip

1、将下载的文件上传pip-1.5.6.tar.gz至python文件夹内

2、解压安装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

3、建立软链接

[root@vm4 pip-1.5.6]#ln -s /usr/python/Python-2.7.3/bin/pip /usr/bin/pip-2.7.3

4、测试

[root@vm4 pip-1.5.6]#pip-2.7.3  install nose

 

五、安装pymongo

python操作mongo时,需要安装此插件

第一种方法

[root@vm4 python]# pip-2.7.3  install pymongo

第二种方法

1、下载文件pymongo-2.7.2.tar.gz并上传至python

2、解压安装

[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读数据的第三方控件;
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

第二种方法

1、下载文件xlrd-0.9.3.tar.gz,xlwt-0.7.5.tar.gz,xlutils-1.7.1.tar.gz并上传至python

2、解压安装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

3、解压安装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

4、解压安装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

 

七、安装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)

 

八、安装过程中遇到的问题

1、在安装了Python2.7.3之后,执行

  1. [root@vm4 Python-2.7.3]#mv /usr/bin/python /usr/bin/python2.6.6  
  2. [root@vm4 Python-2.7.3]#ln -s /usr/python/Python-2.7.3/bin/python /usr/bin/python

再使用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、python2、python2.4三个文件一定不能改变。理论上说yum只是基于python语言,跟版本并没有关系,但是升级过程中确实会出现问题

 

2、在python运行过程中出现如下错误:

python错误:ImportError: No module named setuptools
这句错误提示的表面意思是:没有setuptools的模块,说明python缺少这个模块,那我们只要安装这个
模块即可解决此问题

解决方法:安装setuptools

 

3、执行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

 

4、[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

 

5、执行python脚本时,报./startup.sh: Permission denied

解决方法:chmod u+x *.sh

 

6、[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的关系

很多时候,很多人搞不清楚,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的接口封装得更为友好而已如异常处理。