You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PyInstaller打包含Chargebee API脚本时TLS证书路径错误求助

解决PyInstaller打包Chargebee脚本后TLS CA证书路径错误问题

问题背景

原Python脚本在PyCharm及初始PyInstaller打包(无Chargebee逻辑)时运行正常,添加Chargebee订阅验证逻辑后,PyInstaller单文件打包的exe运行时抛出错误:

"OS Error: Could not find a suitable TLS CA certificate bundle invalid path C:\Users\username\AppData\Local\Temp_MEI70082\chargebee\ssl\ca-certs.crt"

解决方案

方法1:修正--add-data参数并确保资源正确打包

Windows环境下,PyInstaller的--add-data参数需用分号分隔源路径和目标路径,确保源路径指向正确的证书文件。重新执行打包命令:

pyinstaller --onefile --add-data "venv\Lib\site-packages\chargebee\ssl\ca-certs.crt;chargebee\ssl" main.py

若仍无效,改用绝对路径指定源文件:

pyinstaller --onefile --add-data "C:\Users\username\PycharmProjects\YP_Key_package_2\venv\Lib\site-packages\chargebee\ssl\ca-certs.crt;chargebee\ssl" main.py

方法2:在代码中动态指定CA证书路径

PyInstaller单文件模式运行时,会将资源解压到临时目录,该路径可通过sys._MEIPASS获取。修改代码动态指定证书位置:

import sys
import os
import chargebee

# 获取打包后的临时资源目录
if hasattr(sys, '_MEIPASS'):
    bundle_dir = sys._MEIPASS
else:
    bundle_dir = os.path.dirname(os.path.abspath(__file__))

# 构建证书路径
ca_cert_path = os.path.join(bundle_dir, 'chargebee', 'ssl', 'ca-certs.crt')

# 配置Chargebee时指定CA证书
chargebee.configure(site='site-address', api_key='api_key', ca_cert=ca_cert_path)

subscription_key = "subscription_key"

def check_subscription(subscription_key):
    try:
        subscription = chargebee.Subscription.retrieve(subscription_key)
        if subscription.subscription.status == "active":
            fill()
        else:
            print("Error is here1")
            print("Subscription is either not active or is having an issue, please check to make sure your subscription is valid if you continue having issues")
            return False
    except Exception as e:
        print(f"Error: {str(e)}")
        return False

if __name__ == '__main__':
    print("Starting:")
    check_subscription(subscription_key)

修改后重新用--add-data参数打包,确保证书被正确包含。

方法3:使用系统默认CA证书

Chargebee支持使用系统内置的CA证书,无需依赖自带文件。只需在配置时不指定ca_cert:

chargebee.configure(site='site-address', api_key='api_key')
# 移除手动指定ca_cert的代码

这种方式可避免打包时的资源路径问题,适配大多数Windows环境。

内容的提问来源于stack exchange,提问作者jcruzer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.25 17:07:44