centos7.* 安装 python3,pip3

centos7.* 安装 python3,pip3

yum安装方式

添加epel源

1
yum install epel-release

安装Python3.4

1
python34

安装pip3

1
2
yum install python34-setuptools
easy_install-3.4 pip

之后就可以使用pip3

1
pip3

源代码编译安装此方法兼容保留Python2共存

安装依赖环境

1
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

查看最新的Python版本

浏览器打开 https://www.python.org/ftp/python/ 查看最新的Python版本,标记为3.A.B

1
wget https://www.python.org/ftp/python/3.A.B/Python-3.A.B.tgz

创建Python3的目录

1
mkdir /usr/local/python3

解压下载文件并切换目录

1
2
3
tar -zxvf Python-3.A.B.tgz

cd Python-3.A.B

执行

1
2
./configure --prefix=/usr/local/python3
make && make install

创建Python3的软链接

1
ln -s /usr/local/python3/bin/python3 /usr/bin/python3

创建Pip3的软链接

1
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3

测试命令 python3 和 pip3

1
2
python3
pip3

总体安装 3.6.6的脚本:

1
2
3
4
5
6
7
8
9
10
11
12
yum -y install gcc automake autoconf libtool make
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
wget https://www.python.org/ftp/python/3.6.6/Python-3.6.6.tgz
mkdir /usr/local/python3
tar -zxvf Python-3.6.6.tgz
cd Python-3.6.6
./configure --prefix=/usr/local/python3
make && make install
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
ln -s /usr/local/python3/bin/pip3 /usr/bin/pip3
python3 -V
pip3 -V

稍作修改就变成了 2.7 的安装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
wget https://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
tar vxf Python-2.7.6.tgz
cd Python-2.7.6
./configure --prefix=/usr/local
make && make install
/usr/local/bin/python2.7 -V
mv /usr/bin/python /usr/bin/python2.6.6
ln -s /usr/local/bin/python2.7 /usr/bin/python
python -V
#sed -i "s/#!//usr//bin//python/#!//usr//bin//python2.6.6/g" /usr/bin/yum
wget https://bootstrap.pypa.io/ez_setup.py -O - | python
easy_install pip
rm -rf /usr/bin/{pip,pip2}
ln -s /usr/local/bin/pip /usr/bin/pip
ln -s /usr/local/bin/pip /usr/bin/pip2
#vim /usr/bin/yum
# 将文件头部的
# #!/usr/bin/python

# 改成
# #!/usr/bin/python2.6.6