ImportError: 无法从 ‘typing_extensions’ 导入名称 ‘Iterator’

当我在 Google Colab 上尝试安装 openai 时,出现了以下错误:

from openai import OpenAI---------------------------------------------------------------------------ImportError                               Traceback (most recent call last)<ipython-input-6-5ee9a4e68b89> in <cell line: 1>()----> 1 from openai import OpenAI5 frames/usr/local/lib/python3.10/dist-packages/openai/_utils/_streams.py in <module>      1 from typing import Any----> 2 from typing_extensions import Iterator, AsyncIterator      3       4       5 def consume_sync_iterator(iterator: Iterator[Any]) -> None:ImportError: cannot import name 'Iterator' from 'typing_extensions' (/usr/local/lib/python3.10/dist-packages/typing_extensions.py)---------------------------------------------------------------------------NOTE: If your import is failing due to a missing package, you canmanually install dependencies using either !pip or !apt.To view examples of installing some common dependencies, click the"Open Examples" button below.

我的复现详情如下:

Python 3.10.12

操作系统Windows 11

typing_extensions 版本是 4.9.0

编辑:

我的 typing_extensions 详情:

enter image description here


回答:

我在 OpenAI 论坛上找到了一个相关的问题:

https://community.openai.com/t/error-while-importing-openai-from-open-import-openai/578166/26

解决方案 1

感谢 @Aymane_oub

pip install --force-reinstall typing-extensions==4.5pip install --force-reinstall openai==1.8

解决方案 2

感谢 @Shannon-21

在浏览了答案后,我发现可以通过以下方式更改尝试从 typing_extensions 导入 Iterator 的文件的导入(在 Google Colab 上):

%%writefile /usr/local/lib/python3.10/dist-packages/openai/_utils/_streams.pyfrom typing import Anyfrom typing_extensions import AsyncIteratorfrom typing import Iterator # 从正确的库导入 Iteratordef consume_sync_iterator(iterator: Iterator[Any]) -> None:    for _ in iterator:        ...async def consume_async_iterator(iterator: AsyncIterator[Any]) -> None:    async for _ in iterator:        ...

#outputenter image description here

在我的 Colab 单元格中运行上述代码后,我能够使用:

enter image description here

答案链接:https://community.openai.com/t/error-while-importing-openai-from-open-import-openai/578166/27?u=rubeenatalha313

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注