当我在 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
详情:
回答:
我在 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: ...
在我的 Colab 单元格中运行上述代码后,我能够使用: