When building executables with PyInstaller for Python applications that use aiohttp and SSL, you need to ensure the SSL certificates and configuration are bundled properly for the app to function correctly. This article provides guidance on the key steps.
Include certifi with PyInstaller
The
a = Analysis(['your_app.py'],
hiddenimports=['certifi'],
hookspath=[])
Bundle CA file location
By default,
When freezing to an executable, the environment is isolated, so you need to configure
import certifi
import aiohttp
ssl_context = aiohttp.ClientSSLContext()
ssl_context.load_verify_locations(cafile=certifi.where())
OpenSSL linking on Linux
If building a Linux executable, you may hit an error about missing OpenSSL libraries. To resolve this:
- Install
openssl-devel system package - Set the
LDFLAGS option in PyInstaller to link the OpenSSL libraries:
pyinstaller --add-data certifi:certifi \
--hidden-import certifi \
--runtime-tmpdir . \
--ldflags '-Wl,-rpath,$$ORIGIN/lib' \
your_app.py
The key part is
Avoid openssl.cnf issues
Bundling the OpenSSL config file
Avoid this by specifying the
import os
os.environ['OPENSSL_CONF']=os.path.dirname(certifi.where()) + '/openssl.cnf'
Troubleshooting Issues
Some common issues to check if your app doesn't connect properly over SSL after bundling with PyInstaller:
Bundling SSL resources properly while freezing apps with PyInstaller takes some trial-and-error, but these tips should help troubleshoot and get everything working smoothly.