Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux 配置 Python 環境完整步驟(源碼編譯安裝 Python 2.7.3)

編輯:關於 Linux

在 Oracle Linux Enterprise 5.6(或其他舊版 RHEL/CentOS)上,系統預設 Python 版本較舊(如 2.4)。若需較新版本,建議源碼編譯安裝至獨立目錄(/usr/local/python2.7.3),避免衝突系統工具(如 yum)。本文記錄完整編譯安裝與 setuptools 部署流程。

軟體版本

  • Python:Python-2.7.3.tgz
  • setuptools:setuptools-0.6c11.tar.gz

1. 下載源碼

wget https://python.org/ftp/python/2.7.3/Python-2.7.3.tgz
wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.6c11.tar.gz

2. 安裝 Python 2.7.3

mkdir /python
cp Python-2.7.3.tgz /python/
cd /python
tar -zvxf Python-2.7.3.tgz
cd Python-2.7.3

# 指定 prefix 避免污染系統 python
./configure --prefix=/usr/local/python2.7.3
make
make install

3. 安裝 setuptools(提供 easy_install、pkg_resources)

cd /python
tar -zvxf setuptools-0.6c11.tar.gz
cd setuptools-0.6c11
/usr/local/python2.7.3/bin/python setup.py install

4. 驗證安裝

/usr/local/python2.7.3/bin/python -V
# Python 2.7.3
/usr/local/python2.7.3/bin/easy_install --version
# setuptools 版本資訊

5. 使用建議

  • 不要 將 /usr/bin/python 指向新版本,會導致 yum 等系統工具失效。
  • 腳本開頭指定 #!/usr/local/python2.7.3/bin/python。
  • 或建立別名:alias python27='/usr/local/python2.7.3/bin/python'。
  • 虛擬環境:/usr/local/python2.7.3/bin/virtualenv myenv(需先安裝 virtualenv)。

Python 編譯安裝過程截圖

延伸:升級至 Python 3.x

同樣流程,下載 Python-3.x.x.tgz,./configure --prefix=/usr/local/python3.x 編譯安裝。Python 3.3+ 內建 venv 模組,無需額外安裝 virtualenv。

Copyright © Windows教程網 All Rights Reserved