本文主要介绍安装python3的安装,通过python3.6.4源码安装,包括python3的安装步骤介绍以及安装脚本。并且兼容python2, 实现python2与python3共存。

一键安装python3脚本,脚本如下,

yum install -y wget epel-release xz gcc zlib zlib-devel openssl-devel bzip2-devel expat-devel gdbm-devel readline-devel sqlite-devel
if [[ ! -s /usr/bin/python3 ]]; then
        wget http://file.aionlife.xyz/source/download?id=5b9e7227dc72d90ebb47023a -O Python-3.6.4.tar.xz  #https://www.python.org/ftp/python/3.6.4/Python-3.6.4.tar.xz(这个地址国内下载比较慢,所以这里换成另一个地址)
        tar -Jxvf Python-3.6.4.tar.xz
        cd Python-3.6.4
        ./configure --prefix=/usr/python3.6
        make&&make install
        ln -s /usr/python3.6/bin/python3 /usr/bin/python3
        mkdir ~/.pip
        echo -e "[global]\nindex-url = http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host = mirrors.aliyun.com" > ~/.pip/pip.conf
        ln -s /usr/python3.6/bin/pip3 /usr/bin/pip3  
fi

上面脚本可以用vi保存成installpy3.sh 或者也可以执行如下命令来下载脚本

wget http://file.aionlife.xyz/source/download?id=5bac8dacdc72d91ff8d539fd -O installpy3.sh

执行安装脚本

bash installpy3.sh

执行下python3和pip3,如下提示就是安装成功了,原来的python版本还可以正常使用。

[root@kvm4 ~]# python3
Python 3.6.4 (default, Jul 13 2018, 19:40:36)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-28)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[root@kvm4 ~]# pip3
Usage:
pip [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
list List installed packages.
show Show information about installed packages.
check Verify installed packages have compatible dependencies.
search Search PyPI for packages.
wheel Build wheels from your requirements.
hash Compute hashes of package archives.
completion A helper command used for command completion.
help Show help for commands.
General Options:
-h, --help Show help.
--isolated Run pip in an isolated mode, ignoring
environment variables and user configuration.
-v, --verbose Give more output. Option is additive, and can be
used up to 3 times.
-V, --version Show version and exit.
-q, --quiet Give less output. Option is additive, and can be
used up to 3 times (corresponding to WARNING,
ERROR, and CRITICAL logging levels).
--log Path to a verbose appending log.
--proxy Specify a proxy in the form
[user:passwd@]proxy.server:port.
--retries Maximum number of retries each connection should
attempt (default 5 times).
--timeout Set the socket timeout (default 15 seconds).
--exists-action Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host Mark this host as trusted, even though it does
not have valid or any HTTPS.
--cert Path to alternate CA bundle.
--client-cert Path to SSL client certificate, a single file
containing the private key and the certificate
in PEM format.
--cache-dir
    Store the cache data in
      .
      --no-cache-dir Disable the cache.
      --disable-pip-version-check
      Don't periodically check PyPI to determine
      whether a new version of pip is available for
      download. Implied with --no-index.