Linux安裝python2.7


一、Python安裝

以Python-2.7.7為例,安裝包:Python-2.7.7.tgz如無特殊說明,以下安裝步驟都采用root用戶執行

1. 解壓Python-2.7.7.tgz

tar -xzvf Python-2.7.7.tgz

 

2. 進入解壓產生的Python目錄

cd Python-2.7.7

 

3. 安裝

此時可以先安裝zlib模塊,見2.安裝message模塊

./configure --prefix=/usr/local/Python2.7 --enable-shared -enable-unicode=ucs4

configure是源碼安裝的第一步,主要的作用是對即將安裝的軟件進行配置,檢查當前的環境是否滿足安裝軟件的依賴關系。

關於configure的介紹:http://blog.csdn.net/luckywang1103/article/details/18674231

關於-enable-shared的介紹:http://blog.csdn.net/z1988316/article/details/7894734

-enable-unicode=ucs4:為了解決RHL6.6編碼采用ucs4編碼的問題,RHL5.5不需要

2) 執行時出現沒有編譯器錯誤,安裝g++:

yum install gcc-c++

然后重新執行configure命令

 

make

編譯源代碼並生成執行文件

 

make install

把生成的文件拷貝到Linux系統必要的目錄下,如/usr/local/bin,這樣所有用戶都可以運行程序。

 

4. python解釋器指向python2.7

執行python,發現仍然使用Linux自帶的python2.6.6

執行which python,找到python解釋器地址,為:/usr/bin/python

將解釋器指向新安裝的python2.7.7,執行:

cd /usr/bin

rm python

ln -s /usr/local/Python2.7/bin/python python

再執行python,發現缺少python的lib庫:

python: error while loading shared libraries: libpython2.7.so.1.0: cannot open shared object file: No such file or directory

解決方法1:

執行:vi /edt/ld.so.conf

添加libpython2.7.so.1.0的路徑: /usr/local/Python2.7/lib

執行:/sbin/ldconfig -v

執行:ldd libpython2.7.so.1.0,可以找到:

/usr/local/Python2.7/lib:libpython2.7.so.1.0 -> libpython2.7.so.1.0

解決方法2:(我的環境采用解決方法1還是不正確)

cd /etc/ld.so.conf.d/ echo "/usr/local/Python2.7/lib" > python2.7.conf ldconfig

 

執行:python,顯示:

Python 2.7.7 (default, Dec 15 2014, 13:30:16)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> 

表示解釋器已經正確指向python2.7

 

5. 后續工作

由於yum使用了原來python2.6的一些腳本,需要將yum重新導向原來的python2.6解釋器才可以使用

執行:which yum,找到yum位置:/usr/bin/yum

編輯:vi /usr/bin/yum,修改路徑為: #!/usr/bin/python2.6

 

python2.7安裝完成。

下面介紹一些python模塊的安裝:message,cx_Oracle, cython

 

二、安裝message模塊

安裝包:setuptools-7.0,message-0.2.1.tar.gz

1. 安裝setuptools

setuptools是 Python Enterprise Application Kit(PEAK)的一個副項目,可以讓程序員更方便的創建和發布 Python 包。

當需要安裝第三方python包時,可能會用到easy_install命令。easy_install是由PEAK(Python Enterprise Application Kit)開發的setuptools包里帶的一個命令,所以使用easy_install實際上是在調用setuptools來完成安裝模塊的工作。所以如果要使用easy_install的命令就需要安裝setuptools。

解壓:tar -xzvf setuptools-7.0.tar.gz

進入目錄:cd setuptools-7.0

編譯:python setup.py build

安裝:python setup.py install

執行安裝時報錯:

RuntimeError: Compression requires the (missing) zlib module

缺少zlib模塊,zlib模塊用來壓縮和解壓縮數據,以便保存和傳輸

安裝:yum install zlib-devel

這里報錯則需要重新進行python的make和makefile,所以建議在安裝python之前就安裝zlib

重新安裝setuptools。

2. 安裝message模塊

解壓:tar -xzvf message-0.2.2.tar.gz

進入目錄:cd message-0.2.1

編譯:python setup.py build

安裝:python setup.py install

message模塊安裝完成。

 

3. 安裝cx_Oracle

安裝包:cx_Oracle-5.1.2-11g-py27-1.x86_64.rpm

安裝cx_Oracle時我們使用rpm包,這樣在RHL6.6下更方便安裝,不用考慮太多兼容性問題

執行:rpm -ivh cx_Oracle-5.1.2-11g-py27-1.x86_64.rpm

執行:python,輸入:import cx_Oracle

報錯:

>>> import cx_Oracle

Traceback (most recent call last):File "<stdin>", line 1, in <module>

ImportError: No module named cx_Oracle

發現是python2.7下沒有cx_Oracle.so動態鏈接庫,cx_Oracle.so被放在了:/usr/lib/python2.7/site-packages

采用軟鏈接方式:

ln -s /usr/lib/python2.7/site-packages/cx_Oracle.so /usr/local/Python2.7/lib/python2.7/site-packages/cx_Oracle.so

再次執行:python,輸入:import cx_Oracle

會出現找不到oracle_client導致的問題,此時可以在環境變量中配置oracle_client,或在已經配好的用戶下執行,

修改環境變量:vi .bash_profile 配置信息: export ORCL_HOME=/usr/lib/oracle/11.2/client***** export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$ORCL_HOME/lib export TNS_ADMIN=$ORCL_HOME/network/admin export PATH=$PATH:$ORCL_HOME/bin ORCL_HOME的具體路徑以本地安裝路徑配置

source .bash_profile

成功:

Python 2.7.7 (default, Dec 15 2014, 13:30:16)

[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import cx_Oracle

>>> 

cx_Oracle安裝完成。

 

4. 安裝cython

cython介紹見:http://docs.cython.org/en/latest/

安裝包:cython-master.zip

解壓:unzip -o cython-master.zip

進入目錄:cd cython-master

安裝:python setup.py install

可能出現錯誤:/usr/bin/ld: cannot find -lpython2.7, 這個錯誤的原因是庫文件並沒有導入的ld檢索目錄中,在/usr/lib, /usr/local/lib,或者其他自定義的lib下沒有libpython2.7.so文件,實際在 /etc/ld.so.conf 我自定義的lib路徑:/usr/local/Python2.7/lib下是有該文件的,原因未知

解決辦法是重新建立軟鏈接:

ln -s /usr/local/Python2.7/lib/libpython2.7.so.1.0 /usr/local/lib/libpython2.7.so

重新安裝cython:python setup.py install

和之前同樣的道理找不到cython解釋器,在/usr/local/bin下建立軟鏈接:

ln -s /usr/local/Python2.7/bin/cython /usr/local/bin/cython

執行:cython,顯示:

Cython (http://cython.org) is a compiler for code written in the Cython language.  Cython is based on Pyrex by Greg Ewing.
Usage: cython [options] sourcefile.{pyx,py} ...
Options:   -V, --version                  Display version number of cython compiler   -l, --create-listing           Write error messages to a listing file   -I, --include-dir <directory>  Search for include files in named directory                                  (multiple include directories are al

(以下省略)

表示可以正常使用。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM