python3.8 gevent openai 报错 openai.APIConnectionError: Connection error.

现象

python3.8.17版本中同时使用geventopenai 1.53.0包时,openai包报错openai.APIConnectionError: Connection error.

报错信息:

Traceback (most recent call last):
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 990, in _request
    response = self._client.send(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpx/_client.py", line 926, in send
    response = self._send_handling_auth(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpx/_client.py", line 954, in _send_handling_auth
    response = self._send_handling_redirects(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpx/_client.py", line 991, in _send_handling_redirects
    response = self._send_single_request(request)
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpx/_client.py", line 1027, in _send_single_request
    response = transport.handle_request(request)
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpx/_transports/default.py", line 236, in handle_request
    resp = self._pool.handle_request(req)
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_sync/connection_pool.py", line 256, in handle_request
    raise exc from None
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_sync/connection_pool.py", line 236, in handle_request
    response = connection.handle_request(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_sync/connection.py", line 101, in handle_request
    raise exc
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_sync/connection.py", line 78, in handle_request
    stream = self._connect(request)
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_sync/connection.py", line 156, in _connect
    stream = stream.start_tls(**kwargs)
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_backends/sync.py", line 170, in start_tls
    raise exc
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/httpcore/_backends/sync.py", line 165, in start_tls
    sock = ssl_context.wrap_socket(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/ssl.py", line 500, in wrap_socket
    return self.sslsocket_class._create(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/ssl.py", line 1004, in _create
    super(SSLSocket, self).__init__(**kwargs)
TypeError: super(type, obj): obj must be an instance or subtype of type

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_utils/_utils.py", line 274, in wrapper
    return func(*args, **kwargs)
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/resources/chat/completions.py", line 815, in create
    return self._post(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 1277, in post
    return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls))
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 954, in request
    return self._request(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 1014, in _request
    return self._retry_request(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 1092, in _retry_request
    return self._request(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 1014, in _request
    return self._retry_request(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 1092, in _retry_request
    return self._request(
  File "/opt/anaconda3/envs/test3.8/lib/python3.8/site-packages/openai/_base_client.py", line 1024, in _request
    raise APIConnectionError(request=request) from err
openai.APIConnectionError: Connection error.

版本信息

python 3.8.17
httpx 0.27.2
opena 1.53.0

原因

这是一个 ssl.wrap_socket 被 gevent monkey patch 后出现的兼容性问题,常见于 Python 3.8 + gevent + httpx(OpenAI SDK 1.x 默认使用 httpx)组合下。

解决方法

解决方案一:确保最早 monkey.patch_all()

在项目最开始(入口脚本的最上方)调用 gevent 的 monkey patch,如下:

from gevent import monkey
monkey.patch_all()

# 之后再导入 openai 等其他库
import openai

放错位置会导致 socket/ssl 没被正确 patch,出现你这种 ssl 报错。

解决方案二:升级python到3.9或更高

(myenv) PS D:\dachuang> python chatgpt_api_demo.py 输入&#39;exit&#39;退出,&#39;clear&#39;清空历史 你:hello AI思考中... Traceback (most recent call last): File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_transports\default.py", line 101, in map_httpcore_exceptions yield File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_transports\default.py", line 250, in handle_request resp = self._pool.handle_request(req) File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_sync\connection_pool.py", line 256, in handle_request raise exc from None File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_sync\connection_pool.py", line 236, in handle_request response = connection.handle_request( File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_sync\http_proxy.py", line 288, in handle_request connect_response = self._connection.handle_request( File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_sync\connection.py", line 101, in handle_request raise exc File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_sync\connection.py", line 78, in handle_request stream = self._connect(request) File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_sync\connection.py", line 156, in _connect stream = stream.start_tls(**kwargs) File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_backends\sync.py", line 170, in start_tls raise exc File "D:\anaconda\envs\myenv\lib\contextlib.py", line 131, in __exit__ self.gen.throw(type, value, traceback) File "D:\anaconda\envs\myenv\lib\site-packages\httpcore\_exceptions.py", line 14, in map_exceptions raise to_exc(exc) from exc httpcore.ConnectError: TLS/SSL connection has been closed (EOF) (_ssl.c:1149) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "D:\anaconda\envs\myenv\lib\site-packages\openai\_base_client.py", line 982, in request response = self._client.send( File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_client.py", line 914, in send response = self._send_handling_auth( File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_client.py", line 942, in _send_handling_auth response = self._send_handling_redirects( File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_client.py", line 979, in _send_handling_redirects response = self._send_single_request(request) File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_client.py", line 1014, in _send_single_request response = transport.handle_request(request) File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_transports\default.py", line 250, in handle_request resp = self._pool.handle_request(req) File "D:\anaconda\envs\myenv\lib\contextlib.py", line 131, in __exit__ self.gen.throw(type, value, traceback) File "D:\anaconda\envs\myenv\lib\site-packages\httpx\_transports\default.py", line 118, in map_httpcore_exceptions raise mapped_exc(message) from exc httpx.ConnectError: TLS/SSL connection has been closed (EOF) (_ssl.c:1149) The above exception was the direct cause of the following exception: Traceback (most recent call last): File "chatgpt_api_demo.py", line 44, in call_chatgpt response = client.chat.completions.create( File "D:\anaconda\envs\myenv\lib\site-packages\openai\_utils\_utils.py", line 286, in wrapper return func(*args, **kwargs) File "D:\anaconda\envs\myenv\lib\site-packages\openai\resources\chat\completions\completions.py", line 1147, in create return self._post( File "D:\anaconda\envs\myenv\lib\site-packages\openai\_base_client.py", line 1259, in post return cast(ResponseT, self.request(cast_to, opts, stream=stream, stream_cls=stream_cls)) File "D:\anaconda\envs\myenv\lib\site-packages\openai\_base_client.py", line 1014, in request raise APIConnectionError(request=request) from err openai.APIConnectionError: Connection error. During handling of the above exception, another exception occurred: Traceback (most recent call last): File "chatgpt_api_demo.py", line 72, in <module> call_chatgpt() File "chatgpt_api_demo.py", line 64, in call_chatgpt except (APIConnectionError, Timeout): # 此处直接使用Timeout(已正确导入) TypeError: catching classes that do not inherit from BaseException is not allowed (myenv) PS D:\dachuang> python chatgpt_api_demo.py
最新发布
10-19
<think>我们面对的问题是在使用`python -m pip install gevent`时遇到错误,错误信息包括"Installing build dependencies ... error: subprocess-exited-with-error"。根据引用[2]提供的错误信息,我们可以看到更详细的错误描述: 错误提示中提到了Cargo(Rust的包管理器)没有安装或不在PATH中,因为gevent的安装需要Rust工具链来编译扩展。 因此,解决这个问题的关键在于安装Rust工具链(包括Cargo)并确保它在系统的PATH环境变量中。 具体步骤: 1. 安装Rust:按照官方推荐的方式,通过访问https://rustup.rs/来安装Rust。这个网站提供了适合不同操作系统的安装脚本。 - 对于Windows系统,可以下载rustup-init.exe并运行。 - 对于Linux或macOS,可以使用命令:`curl --proto &#39;=https&#39; --tlsv1.2 -sSf https://sh.rustup.rs | sh` 2. 安装完成后,重新启动终端(或命令提示符/PowerShell),以确保环境变量生效。 3. 验证Rust和Cargo是否安装成功:运行`rustc --version`和`cargo --version`,如果都能显示版本信息,则说明安装成功。 4. 再次尝试安装gevent:`python -m pip install gevent` 另外,引用[1]中提到了另一个错误(关于pycryptodome的),但当前问题主要是gevent安装需要Rust。如果安装gevent后仍然出现其他包的安装错误,可以再针对具体错误解决。 注意:有时在Windows上,可能需要安装Visual Studio Build Tools(因为Rust在Windows上依赖MSVC工具链)。在Linux上,通常需要安装build-essential等基础构建工具。在macOS上,可能需要安装Xcode命令行工具。 总结:安装Rust工具链是解决这个问题的关键。 如果用户按照上述步骤操作后仍然遇到问题,可能需要检查系统环境变量是否包含Rust的工具链路径(通常是`$HOME/.cargo/bin`),或者查看详细的错误日志。 相关问题: 1. 如何安装Rust编程语言? 2. 在安装Python包时,遇到需要编译扩展的情况,一般需要什么前置条件? 3. 如何设置系统PATH环境变量? 4. 在Windows/Linux/macOS上安装Python包时,常见的编译工具链有哪些?</think>### 解决 `python -m pip install gevent` 报错指南 根据错误信息分析,问题核心是 **缺少 Rust 编译工具链**(`Cargo` 和 `Rustc`)。Gevent 在安装时需要编译 Rust 扩展,但系统未检测到 Rust 环境[^2]。以下是完整解决方案: #### 步骤 1:安装 Rust 工具链 ```bash # Linux/macOS 安装命令 curl --proto &#39;=https&#39; --tlsv1.2 -sSf https://sh.rustup.rs | sh # Windows 用户 下载安装包:https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-msvc/rustup-init.exe ``` 安装完成后**重启终端**,运行验证命令: ```bash rustc --version # 应显示类似 rustc 1.76.0 (07dca489a 2024-02-04) cargo --version # 应显示类似 cargo 1.76.0 (1dcdb7d7b 2024-02-11) ``` #### 步骤 2:安装系统编译依赖 | 系统 | 所需依赖包 | 安装命令 | |------------|-----------------------------|--------------------------------------------------------------------------| | **Ubuntu/Debian** | build-essential, python3-dev | `sudo apt-get install build-essential python3-dev` | | **CentOS/RHEL** | gcc, make, python3-devel | `sudo yum groupinstall "Development Tools" && sudo yum install python3-devel` | | **macOS** | Xcode 命令行工具 | `xcode-select --install` | | **Windows** | Microsoft C++ 生成工具 | 安装 Visual Studio 2022 并勾选 "使用 C++ 的桌面开发" | #### 步骤 3:重试安装 Gevent ```bash python -m pip install --upgrade pip setuptools wheel # 更新基础工具 python -m pip install gevent --no-cache-dir # 禁用缓存重试安装 ``` #### 备选方案:使用预编译二进制 如果仍失败,尝试从预编译包安装: ```bash python -m pip install gevent --only-binary=:all: # 强制使用二进制包 ``` #### 验证安装 ```python import gevent print(gevent.__version__) # 成功时应显示版本号 ``` > **关键提示**:错误信息中 `Cargo, the Rust package manager, is not installed` 明确指向 Rust 缺失[^2]。安装 Rust 后需重启终端使环境变量生效。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无糖可乐·

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值