有些时候,由于操作系统的不完整,我们无法直接安装binary的Python,而需要从Python源代码进行编译和安装。
以Redhat系统从源安装Python2.7.12为例。
# Step 1, Install compilers and related tools
yum groupinstall -y "development tools"
# Step 2, Install libraries needed to compile all features of Python
yum install -y zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel expat-devel
# Step 3, Download python 2.7.12
wget http://python.org/ftp/python/2.7.12/Python-2.7.12.tar.xz
# Step 4, Decompress the downloaded file
tar xf Python-2.7.12.tar.xz
cd Python-2.7.12
# Step 5, Configure python 2.7.12 source code
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# Step 6, Install python 2.7.12
make && make altinstall
# Step 7, Set python 2.7.12 as default python
sudo rm -rf /usr/bin/python
ln -s /usr/local/bin/python2.7 /usr/bin/python
# Step 8, Download pip install script
wget --no-check-certificate https://bootstrap.pypa.io/get-pip.py
# Step 9, Install pip
python get-pip.py --index-url=http://pypi.python.org/simple/ --trusted-host pypi.python.org
# Step 10, Make Sure yum configuration is NOT changed
sed '1d' /usr/bin/yum > tmpfile; sed -i '1 i\#!/usr/bin/python2.6' tmpfile; mv tmpfile /usr/bin/yum; sudo chmod 755 /usr/bin/yum
在第6步中,有可能出现make
(等同于make all
)命令无线循环的问题。这时,可以参考这个网页:https://stackoverflow.com/questions/11179203/python-build-goes-into-infinite-loop。例如,使用date
命令,将系统时间修改成当前时间:
date +%Y%m%d -s "20170809"