linux中安装python3

备份并替换阿里源

1
tar -zcvf CentOS-bk.tar.gz /etc/yum.repos.d/CentOS-*
1
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
1
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

安装依赖

1
yum -y groupinstall "Development tools"
1
yum install -y ncurses-devel gdbm-devel xz-devel sqlite-devel tk-devel uuid-devel readline-devel bzip2-devel libffi-devel
1
yum install -y openssl-devel openssl11 openssl11-devel

下载python 网地址:https://www.python.org/

1
mkdir -p /doc/temp && cd /doc/temp
1
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tgz

编译

1
2
export CFLAGS=$(pkg-config --cflags openssl11)
export LDFLAGS=$(pkg-config --libs openssl11)

解压编译安装

1
tar xvzf Python-3.10.4.tgz
1
cd Python-3.10.4
1
./configure --enable-optimizations && make altinstall

验证

1
/usr/local/bin/python3.10 --version
1
/usr/local/bin/pip3.10 --version

配置

1
ln -sf /usr/local/bin/python3.10 /usr/bin/python3
1
ln -sf /usr/local/bin/pip3.10  /usr/bin/pip3

pip源

1
mkdir -p ~/.pip
1
touch ~/.pip/pip.conf

编辑配置文件

vim ~/.pip/pip.conf

1
2
3
4
5
6
7
8
9
10
11
12
13
```python
[global]
index-url=https://pypi.tuna.tsinghua.edu.cn/simple/
extra-index-url=
http://pypi.douban.com/simple/
http://mirrors.aliyun.com/pypi/simple/
#proxy = [user:passwd@]proxy.server:port
[install]
trusted-host=
pypi.tuna.tsinghua.edu.cn
pypi.douban.com
mirrors.aliyun.com
ssl_verify: false

上面配置了清华,豆瓣,阿里的源,并且关闭了ssl验证。当然如果有需要还可以设置代理,把注释掉的proxy那行放开即可。

再验证一下

1
pip3 install --upgrade pip