使用PyInstaller打包后Stripe TLS CA证书缺失问题求助
解决PyArmor+PyInstaller打包Stripe程序后TLS CA证书缺失问题
问题原因
打包过程中,Stripe模块自带的ca-certificates.crt证书文件未被正确嵌入到exe的临时运行目录,导致程序运行时无法找到证书文件发起TLS请求。
解决方案
方法1:手动指定本地CA证书
- 下载标准CA证书 bundle,保存为
ca-certificates.crt放到项目根目录。 - 在代码开头添加配置,强制Stripe使用本地证书:
import stripe import os # 绑定本地证书路径 stripe.ca_bundle = os.path.join(os.path.dirname(__file__), 'ca-certificates.crt') - 打包时确保证书被包含:
使用PyInstaller命令行添加数据文件:
或修改spec文件的pyinstaller --add-data "ca-certificates.crt;." your_script.pydatas字段:
之后再用PyArmor处理打包后的产物。a = Analysis(...) a.datas += [('ca-certificates.crt', './ca-certificates.crt', 'DATA')]
方法2:强制打包Stripe自带证书
- 生成PyInstaller的spec文件:
pyi-makespec your_script.py - 打开spec文件,添加Stripe证书的路径映射:
import stripe from pathlib import Path # 获取Stripe自带证书的绝对路径 stripe_cert = str(Path(stripe.__file__).parent / 'data' / 'ca-certificates.crt') a = Analysis( ['your_script.py'], datas=[(stripe_cert, 'stripe/data')], # 保持原目录结构打包 # 其他原有配置... ) - 用spec文件打包:
最后用PyArmor处理生成的exe文件。pyinstaller your_script.spec
方法3:使用系统默认CA证书
直接让Stripe调用系统自带的CA证书存储,无需依赖模块内的证书文件:
import stripe # 启用系统CA证书 stripe.ca_bundle = True
此方法无需额外打包证书文件,兼容性依赖系统环境的CA配置。
内容的提问来源于stack exchange,提问作者The DEV
相关产品推荐
相关产品推荐

