CentOS 6.x系统默认安装的Python是2.6版本,目前很多流行的库都要求Python版本不低于2.7。
以下脚本是对Python-2.7.13安装过程的封装:
#!/bin/bash
START=$(date +%s)
CPU_CORES=`getconf _NPROCESSORS_ONLN`
PYTHON_VERSION="Python-2.7.13"
SRC_DIR="/usr/local/src"
SCRIPT_DIR="/usr/local/scripts"
PREFIX="/usr/local/python2.7.13"
CONFIGURE_PARAMS="--prefix=${PREFIX}"
DONE="[\e[0;32mDONE\e[0m]"
FAILED="[\e[0;31mFAILED\e[0m]"
# is user root?
if [[ $EUID -ne 0 ]] ; then
echo "Error: This script must be run with root access."
exit 1
fi
echo -n "Installing dependencies ... "
yum -y install gcc gcc-c++ autoconf automake &>/dev/null
echo -e ${DONE}
# download source
PYTHON_TAR_FILE="${SRC_DIR}/${PYTHON_VERSION}.tgz"
if [[ ! -f ${PYTHON_TAR_FILE} ]] ; then
echo -n "Downloading: ${PYTHON_VERSION} ... "
wget -q "https://www.python.org/ftp/python/${PYTHON_VERSION##*-}/${PYTHON_VERSION}.tgz" -O ${PYTHON_TAR_FILE}
if [[ ! -s ${PYTHON_TAR_FILE} ]] ; then
rm -f ${PYTHON_TAR_FILE}
echo -e ${FAILED}
exit 1
fi
echo -e ${DONE}
fi
# extract
echo -n "Extracting: ${PYTHON_VERSION} ... "
tar zxf ${PYTHON_TAR_FILE} -C ${SRC_DIR}/
echo -e ${DONE}
# install
cd ${SRC_DIR}/${PYTHON_VERSION}
echo "Configuring ${PYTHON_VERSION} ... ${CONFIGURE_PARAMS}"
./configure ${CONFIGURE_PARAMS}
echo "Compiling ${PYTHON_VERSION} ..."
make -j${CPU_CORES}
echo "Installing ${PYTHON_VERSION} ..."
make install
# update link
mv -f /usr/bin/python /usr/bin/python.old
ln -s ${PREFIX}/bin/python /usr/bin/python
# update pip
GET_PIP_SCRIPT="${SCRIPT_DIR}/get-pip.py"
if [[ ! -f ${GET_PIP_SCRIPT} ]] ; then
echo -n "Downloading: get-pip.py ... "
wget -q "https://bootstrap.pypa.io/get-pip.py" -P ${SCRIPT_DIR}
if [[ ! -s ${GET_PIP_SCRIPT} ]] ; then
rm -f ${GET_PIP_SCRIPT}
echo -e ${FAILED}
else
echo -e ${DONE}
${PREFIX}/bin/python ${SCRIPT_DIR}/get-pip.py
mv -f /usr/bin/pip /usr/bin/pip.old
ln -s ${PREFIX}/bin/pip /usr/bin/pip
fi
fi
END=$(date +%s)
DUR=$(( END - START ))
printf "Execution time: %dm%ds\n" $(( DUR / 60 )) $(( DUR % 60 ))
echo "Finished!"
有用的话就赞一下吧!
:-D