1. 下载并解压python3.8源码包

https://www.python.org/
https://www.python.org/downloads/

选择XZ compressed source tarball, 拷贝文件绝对路径链接, 用wget命令下载

wget -c https://www.python.org/ftp/python/3.8.0/Python-3.8.0.tar.xz
xz -d Python-3.8.0.tar.xz
tar -xvf Python-3.8.0.tar

2. 安装依赖

2.1. 安装sqlite3

http://www.sqlite.org/

wget -c https://www.sqlite.org/2019/sqlite-autoconf-3280000.tar.gz
./configure --prefix=/usr/local/
make
make install
2.2. 安装ssl
brew install zlib openssl
ln -s /usr/local/Cellar/zlib/1.2.11/include/* /usr/local/include
ln -s /usr/local/Cellar/zlib/1.2.11/lib/* /usr/local/lib

3. 支持ssl

cd Python-3.8.0/Modules
vim Setup

搜索:

:/ssl

将以下内容取消注释:

# Socket module helper for socket(2)
#_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
#_ssl _ssl.c \
#   -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
#   -L$(SSL)/lib -lssl -lcrypto

取消注释如下:

# Socket module helper for socket(2)
_socket socketmodule.c

# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
#SSL=/usr/local/ssl
_ssl _ssl.c \
    -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
    -L$(SSL)/lib -lssl -lcrypto

4. 编译安装

指定安装目录:

./configure --prefix=/usr/local/python3.8 --enable-loadable-sqlite-extensions

或:

优化选项安装(可选), 执行完上一步后会提示执行以下的代码对Python解释器进行优化,执行该代码后,会编译安装到 /usr/local/bin/ 下,且不用添加软连接或环境变量, 如下:

If you want a release build with all stable optimizations active (PGO, etc),
please run ./configure --enable-optimizations

# ./configure --enable-optimizations

编译安装:

make
sudo make install

5. 创建软链接

sudo ln -s /usr/local/python3.8/bin/python3.8 /usr/bin/python3.8

6. 检测

$ python3.8
Python 3.8.0 (default, Oct 15 2019, 15:00:48)
[Clang 10.0.1 (clang-1001.0.46.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import ssl
>>>