安装 libffi

却少 libffi 会引发 ModuleNotFoundError: No module named '_ctypes'
https://github.com/libffi/libffi/releases
下载 libffi-3.3.tar.gz

tar -zxvf libffi-3.3.tar.gz
cd libffi-3.3
./configure --prefix=/home/tra1/.local
make && make install

安装 openssl

https://github.com/openssl/openssl/releases

tar -zxvf openssl-1.1.1i.tar.gz
cd openssl-1.1.1i/
./Configure --prefix=/home/tra1/.local
./config --prefix=/home/tra1/.local
make && make install

首先下载 Python3.9 源代码

wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz

用任何方式把源码上传到服务器的任意文件夹,并将其解压

tar zxvf Python-3.9.1.tgz

进入解压后的目录

cd Python-3.9.1

设置好要安装的路径,这里选择 /home/tra1/.local
gcc 8.1.0 版本以上

PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig
./configure --enable-optimizations --prefix=/home/tra1/.local --with-openssl=/home/tra1/.local
make && make install

gcc 8.1.0 版本以下

PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig
./configure --prefix=/home/tra1/.local --with-openssl=/home/tra1/.local
make && make install

这里一定要注意gcc版本,gcc 8.1.0 版本以下使用 --enable-optimizations 参数会引发 generate-posix-vars failed 错误

generate-posix-vars failed

make build_all CFLAGS_NODIST=" -fprofile-use -fprofile-correction" LDFLAGS_NODIST=""
make[1]: Entering directory `/usr/local/src/Python-3.8.0'
./python -E -S -m sysconfig --generate-posix-vars ;\
if test $? -ne 0 ; then \
    echo "generate-posix-vars failed" ; \
    rm -f ./pybuilddir.txt ; \
    exit 1 ; \
fi
Could not import runpy module
Traceback (most recent call last):
  File "/usr/local/src/Python-3.8.0/Lib/runpy.py", line 15, in <module>
    import importlib.util
  File "/usr/local/src/Python-3.8.0/Lib/importlib/util.py", line 14, in <module>
    from contextlib import contextmanager
  File "/usr/local/src/Python-3.8.0/Lib/contextlib.py", line 4, in <module>
    import _collections_abc
SystemError: <built-in function compile> returned NULL without setting an error
generate-posix-vars failed
make[1]: *** [pybuilddir.txt] Error 1
make[1]: Leaving directory `/usr/local/src/Python-3.8.0'
make: *** [profile-opt] Error 2
export PATH=$HOME/.local/bin:$HOME/.local/sbin:$PATH

export PKG_CONFIG_PATH=$HOME/.local/lib/pkgconfig:$PKG_CONFIG_PATH

export LD_LIBRARY_PATH=$HOME/.local/lib:$HOME/.local/lib64:$LD_LIBRARY_PATH
export LD_RUN_PATH=$HOME/.local/lib:$HOME/.local/lib64:$LD_RUN_PATH
export LIBRARY_PATH=$HOME/.local/lib:$HOME/.local/lib64:$LIBRARY_PATH

export C_INCLUDE_PATH=$HOME/.local/include
export CPLUS_INCLUDE_PATH=$HOME/.local/include

export CPPFLAGS=-I$HOME/.local/include
export LDFLAGS="-L$HOME/.local/lib -L$HOME/.local/lib64"
https://blog.csdn.net/whatday/article/details/103903955
https://blog.csdn.net/qq_36416904/article/details/79316972