导语:
如果本机已经安装了python2,通常Linux系统自带python2,安装python3和已经有的python2并不会冲突,所以尽量不要动已有的python2环境。
1 安装Python3所依赖的环境包,不安装在Python3使用过程中会出现各种各样的坑
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel
2 去官网下载Python3.6包
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
或者使用curl
curl https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz
3 解压到指定目录
我通常习惯安装在/usr/local/python3路径下,创建这个安装路径
mkdir -p /usr/local/python3
将下载好的Python-3.6.1t.gz包进行解压
tar -zxvf Python-3.6.1.tgz
4 进入解压后的文件夹,编译安装
cd Python-3.6.3tgz
设置安装路径
./configure --prefix=/usr/local/python3
编译安装
make && make install
安装完毕后,在/usr/local/python3路径下执行
./python3
如果进入交互界面则安装成功
5 设置软连接,可以在任何路径下使用python3命令
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
6 并将/usr/local/python3/bin加入PATH
# vim ~/.bash_profile
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
export PATH
按ESC,输入:wq回车退出。
修改完记得执行行下面的命令,让上一步的修改生效:
# source ~/.bash_profile
7 检查Python3及pip3是否正常可用:
python3 -V
Python 3.6.1
pip3 -V
pip 9.0.1 from /usr/local/python3/lib/python3.6/site-packages (python 3.6)
7 不行的话在创建一下pip3的软链接
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3