CENTOS 6.5 安裝 Python 2.7 總結

CENTOS 6.X 系列默認安裝的 Python 2.6 ,目前開發中主要是使用 Python 2.7 ,這兩個版本之間還是有不少差異的,程序在 Python 2.6 下經常會出問題。

yum install libffi-devel libssl-devel libpython2.7-devel mysql-devel ncurses-devel
yum -y install readline-devel
yum -y install patch

比如: re.sub 函數 ,2.7 支持 flags 參數,而 2.6 卻不支持。

所以,打算安裝 Python 2.7 來運行 Flask 應用程序,但 2.6 不能刪除,因爲系統對它有依賴。

1、安裝 sqlite-devel

因爲 Flask 應用程序可能使用能 Sqlite 數據庫,所以這個得裝上(之前因爲沒裝這個,導致 Python 無法導入 sqlite3 庫。

當然,也可以從源碼編譯安裝。

yum install sqlite-devel -y

2、安裝 Python 2.7

注意,安裝前要先安裝一下openssl,不然打不開https網站
sudo yum install openssl-devel


wget https://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
tar xf Python-2.7.8.tgz
cd Python-2.7.8
./configure --prefix=/usr/local
sudo make && sudo make install

安裝成功之後,你可以在 /usr/local/bin/python2.7 找到 Python 2.7。

3、安裝 setuptools + pip

這裏需要注意,一定要使用 python2.7 來執行相關命令。


方便的方法: 

wget  https://bootstrap.pypa.io/get-pip.py

python get-pip.py 這樣就安裝好了

# First get the setup script for Setuptools:
wget https://bitbucket.org/pypa/setuptools/raw/bootstrap/ez_setup.py
wget https://pypi.python.org/packages/ba/2c/743df41bd6b3298706dfe91b0c7ecdc47f2dc1a3104abeb6e9aa4a45fa5d/ez_setup-0.9.tar.gz
python2.7 distribute_setup.py

# Then install it for Python 2.7 這裏一定要用全路徑:
/usr/local/bin/python2.7 ez_setup.py

# Now install pip using the newly installed setuptools:
easy_install-2.7 pip

# With pip installed you can now do things like this:
pip2.7 install [packagename]
pip2.7 install --upgrade [packagename]
pip2.7 uninstall [packagename]

4、使用 virtualenv

# Install virtualenv for Python 2.7 and create a sandbox called my27project:
pip2.7 install virtualenv
virtualenv-2.7 my27project

# Check the system Python interpreter version:
python --version
# This will show Python 2.6.6

# Activate the my27project sandbox and check the version of the default Python interpreter in it:
source my27project/bin/activate
python --version
# This will show Python 2.7.X
deactivate

基本就是這些了,網上很多教程都說要做軟鏈接,但我感覺那樣做或多或少會對系統有一些未知的影響。這個方法能儘量保持系統的完整性,很多自帶 Python 程序其實在頭部都指定了 #!/usr/bin/python ,所以它們用的其實是 Python 2.6 ,而不是新安裝的 Python 2.7 。

原文:http://digwtx.duapp.com/54.html

參考: http://toomuchdata.com/2014/02/16/how-to-install-python-on-centos/

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章