前言

记录下安装过程,以便日后查看。

  • Ubuntu 20.04.2 LTS (使用的Docker ubuntu:latest,可能会与其他环境略有不同。)
  • Python 3.9.5 (发布前最新版本,其他版本操作相通。)

以下操作都是基于ROOT权限下操作,如果不是或者权限不足,请在命令前添加 sudo

环境依赖

  • 切换阿里云镜像源加速,Ubuntu 20.04,网络较好可以跳过,如需其他源请自行搜索替换。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 备份
cp /etc/apt/sources.list /etc/apt/sources.list.backup
# 写入
echo "deb http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-security main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-updates main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-proposed main restricted universe multiverse

deb http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src http://mirrors.aliyun.com/ubuntu/ focal-backports main restricted universe multiverse" > /etc/apt/sources.list
# 更新
apt update && apt upgrade -y
  • 安装编译Python需要的依赖包,全面安装,为了编译后不出现各种问题,宁错杀一千不放过一个包。
1
2
3
4
# 看需求安装 有些包是编译PHP用的 懒得拆分了 有强迫症的可以试试
apt install gcc make wget curl build-essential autoconf automake m4 openssl libreadline-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev -y
apt install libbz2-dev libxml2-dev libjpeg-dev libpng-dev libfreetype6-dev libzip-dev libssl-dev libsqlite3-dev libcurl4-openssl-dev libgmp3-dev libonig-dev libreadline-dev libxslt1-dev libffi-dev -y
# apt install libpcre3-dev libjpeg62-dev libpng12-dev libpng3 libpnglite-dev libiconv-hook-dev libiconv-hook1 libmcrypt-dev libmcrypt4 libmhash-dev libmhash2 libltdl-dev libmysqlclient-dev libmagickcore-dev libmagickwand-dev libedit-dev -y

下载

选择需要的版本下载XZ Compressed source tabball,下载速度慢可以考虑镜像下载。

Python下载官网 | Python3.9.5

Python下载镜像 | Python3.9.5

1
2
wget https://www.python.org/ftp/python/3.9.5/Python-3.9.5.tar.xz
# wget https://npm.taobao.org/mirrors/python/3.9.5/Python-3.9.5.tar.xz

安装

Python3.9.5安装为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 解包,解压缩
xz -d Python-3.9.5.tar.xz
tar -xvf Python-3.9.5.tar

# 进入目录
cd Python-3.9.5

# 编译
./configure --prefix=/usr/local/python3.9.5 --enable-optimizations
# --prefix=/usr/local/python3.9.5 预安装目录
# --enable-optimizations 是优化选项(LTO,PGO 等)加上这个 flag 编译后,性能有 10% 左右的优化(看需求)

# 安装
make && make install

# 创建软链接 方便使用
ln -s /usr/local/python3.9.5/bin/python3.9 /usr/local/bin/python3.9
ln -s /usr/local/python3.9.5/bin/pip3.9 /usr/local/bin/pip3.9

等待编译完成即可,如果没有什么其他的报错,应该就OK的。

使用

使用python3.9 xxx.pypip3.9 install xxxx

怎么喜欢怎么来

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
root@de9bc33b1a58:~# pip3.9 -V
pip 21.1.1 from /usr/local/python3.9.5/lib/python3.9/site-packages/pip (python 3.9)
root@de9bc33b1a58:~# python3.9 -V
Python 3.9.5
root@de9bc33b1a58:~# python3.9
Python 3.9.5 (default, Jun 3 2021, 12:04:16)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> print(requests.get("http://ip-api.com/json").json())
{
"status":"success",
"country":"China",
"countryCode":"*",
"region":"*",
"regionName":"*",
"city":"*",
"zip":"",
"lat":*,
"lon":*,
"timezone":"Asia/Shanghai",
"isp":"*",
"org":"",
"as":"*",
"query":"*"
}
>>>

PIP源

1
2
3
4
5
6
7
8
9
清华:https://pypi.tuna.tsinghua.edu.cn/simple

阿里云:https://mirrors.aliyun.com/pypi/simple/

中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/

豆瓣:https://pypi.doubanio.com/simple/

# pip3.9 install xxxx -i https://pypi.tuna.tsinghua.edu.cn/simple

END.