Currently, Python 2 is installed by default in the latest CentOS 7.3, and Python 3 installation package is not provided in the default official yum source. Recently, we need to install awscli on the machine. We need python2.6.5 + or python3.3 +. I install python3 through source compilation.
My machine version:
cat /etc/redhat-release #CentOS Linux release 7.3.1611 (Core)
python2 version
python --version Python 2.7.5
Source compilation and installation
- Install environment dependency first to facilitate subsequent compilation and installation
# wget is used to download source package # gcc and make for compilation yum install wget gcc make #make reports an error. Python has a very important built-in module, zipimport, which is used to import modules from Zip packages #zipimport.ZipImportError: can't decompress data; zlib not available yum install zlib-devel #make install reports an error, #ModuleNotFoundError: No module named '_ctypes' yum install libffi-devel # Resolve import ssl error No module named '_ssl' yum install openssl-devel # Resolve import bz2 error yum install bzip2-devel # Solve import curses error yum install ncurses-devel # Solve import sqlite3 error yum install sqlite-devel # Resolve the reminder of DBM gdbm missing yum install gdbm-devel # Solve the "lzma missing reminder" yum install xz-devel # Solve the problem of "missing reminder" yum install tk-devel # Solve the problem of missing readline reminder and unexpected behavior of direction key yum install readline-devel
- After the environment is configured, the Python's official website Download source package
wget https://www.python.org/ftp/python/3.7.1/Python-3.7.1.tar.xz
- decompression
xz -d Python-3.7.1.tar.xz tar -xvf Python-3.7.1.tar
- Compile & install
cd Python-3.6.1 #--prefix is the expected installation directory, - enable optimizations is the optimization options (LTO, PGO, etc.) ./configure --prefix=/usr/local/python3.6 --enable-optimizations # install make && make install
- Add soft link
ln -s /usr/local/python3.7/bin/python3.7 /usr/bin/python3 ln -s /usr/local/python3.7/bin/pip3.7 /usr/bin/python3
- View version number
python3 --version #Python 3.7.1 pip3 --version #pip 10.0.1 from /usr/local/python3.7/lib/python3.7/site-packages/pip (python 3.7)
Subsequent installation and upgrade
- The version number of pip3 is relatively low. Upgrade by command
pip3 install --upgrade pip
Then check the version number
pip3 --version #pip 18.1 from /usr/local/python3.7/lib/python3.7/site-packages/pip (python 3.7)
- There is no pip for python2 of centos7, which can be installed through script
wget https://bootstrap.pypa.io/get-pip.py python get-pip.py
View version number
pip --version #pip 18.1 from /usr/lib/python2.7/site-packages/pip (python 2.7)