EDIT/NOTE: Please see the comment below from @oferchen, this is supported properly in the build system now
Get the patch file (python3-static-link-openssl.patch
) and put it in your working directory
$ wget https://www.python.org/ftp/python/3.8.12/Python-3.8.12.tar.xz
$ tar -xvf Python-3.8.12.tar.xz && cd Python-3.8.12
$ patch -p1 < ../python3-static-link-openssl.patch
$ ./configure ... --with-openssl=/opt/openssl-1.1.1k --enable-static-openssl
$ make -j && sudo make install
You can verify OpenSSL was statically linked using the following
$ strace -o LOG python3 -c 'import ssl'
$ grep libssl LOG | wc -l
0
$
with recent version of python you can set the environment variable PY_UNSUPPORTED_OPENSSL_BUILD=static