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

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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