
Stable Diffusion 运行时 SSL 证书验证失败的解决方案
在使用 Stable Diffusion 时,您可能会遇到 SSL 证书验证失败的错误,通常显示为 ssl.SSLCertVerificationError: [ssl: certificate_verify_failed]。 这通常是因为 Python 无法验证服务器 SSL 证书的有效性。 简单地忽略证书验证并非长久之计,因为它会带来安全隐患。
一些用户尝试在 launch.py 中添加 ssl._create_default_https_context = ssl._create_unverified_context 来绕过验证,但仍然遇到错误。 这表明问题并非简单的证书信任问题,而是缺少必要的根证书。
解决方法是安装 certifi 包,它包含 Mozilla 维护的权威根证书集合。
首先,使用 pip 安装 certifi:
pip install certifi
然后,在您的 launch.py 或其他需要 HTTPS 请求的代码中,使用 certifi.where() 获取证书文件路径,并将其传递给 ssl.create_default_context():
import ssl import certifi ssl_context = ssl.create_default_context(cafile=certifi.where())
通过使用 certifi 提供的证书,Python 就能正确验证服务器的 SSL 证书,从而避免 SSL 验证失败错误。 这种方法比直接忽略证书验证更安全可靠。










