How to import openai package using jupyter notebook?

题意:如何在 Jupyter Notebook 中导入 openai 包?

问题背景:

I am getting the below error message when importing openai as ai using google jupyter notebook:

当我在 Google Jupyter Notebook 中导入 openai 并将其命名为 ai 时,出现了以下错误消息:

python 复制代码
ImportError                               Traceback (most recent call last)
<ipython-input-9-3f86bb4abbfc> in <module>
----> 1 import openai as ai

   /opt/anaconda3/lib/python3.8/site-packages/openai/__init__.py in <module>
      4 
      5 import os as _os
----> 6 from typing_extensions import override
      7 
      8 from . import types

ImportError: cannot import name 'override' from 'typing_extensions' 
(/opt/anaconda3/lib/python3.8/site-packages/typing_extensions.py)

I have no idea how to fix this. Any ideas?

我不知道该如何解决这个问题。有什么建议吗?

问题解决:

It seems a problem related to versions that seem incompatible.

这似乎是一个与版本不兼容相关的问题。

I tested a Python 3.8 environment and openai==1.6.1 seems to work fine. Try to install the following pinned version.

我测试了一个 Python 3.8 环境,openai==1.6.1 似乎运行良好。尝试安装以下固定版本。

python 复制代码
python -m pip install openai==1.6.1

EDIT: 编辑

pd: typing_extension is well known to cause a lot of dependcy problems, as this one. I suggest you:

附注:typing_extension 很容易引发许多依赖性问题,就像这个问题一样。我建议你:

  • Pin your version in a requirements.txt to ensure your project work as expected. Here are mines from a recent project. If the above suggestion does not work, use this snippet as the requirements.txt.

requirements.txt 文件中固定你的版本,以确保项目按预期工作。以下是我最近一个项目中的版本。如果上述建议不起作用,可以使用这个片段作为 requirements.txt 文件。

python 复制代码
python-dotenv~=1.0.0
azure-core~=1.29.5
azure-identity~=1.15.0
chainlit~=0.7.604
openai~=1.3.5
langchain~=0.0.339
pypdf~=3.17.1
python-docx~=1.1.0
faiss-cpu
tiktoken
pysqlite3-binary
  • It is almost sure that you will face similar issues soon. So, I suggest you to search the Error trace on Google and dig into the forums (Github issues, Stackoverflow, etc) for answers. For example, you could type the following in google:

几乎可以肯定你很快就会遇到类似的问题。因此,我建议你在 Google 上搜索错误跟踪,并在论坛(如 Github 问题、Stack Overflow 等)中寻找答案。例如,你可以在 Google 中输入以下内容:

python 复制代码
ImportError: cannot import name 'override' from 'typing_extensions'

And you would find this Giuthub issue: ImportError: cannot import name 'override' from 'typing_extensions' with openai==1.2.0 · Issue #751 · openai/openai-python · GitHub which could help you clarify what it is happening.

相关推荐
心本无晴.14 分钟前
Python进程,线程
python·进程
堆栈future2 小时前
我的个人网站上线了,AI再一次让我站起来了
程序员·llm·aigc
即兴小索奇2 小时前
CodePerfAI体验:AI代码性能分析工具如何高效排查性能瓶颈、优化SQL执行耗时?
ai·商业·ai商业洞察·即兴小索奇
java1234_小锋3 小时前
Scikit-learn Python机器学习 - 特征降维 压缩数据 - 特征提取 - 主成分分析 (PCA)
python·机器学习·scikit-learn
java1234_小锋3 小时前
Scikit-learn Python机器学习 - 特征降维 压缩数据 - 特征提取 - 线性判别分析 (LDA)
python·机器学习·scikit-learn
大模型教程4 小时前
AI Agent 发展趋势与架构演进
程序员·llm·agent
思辨共悟4 小时前
Python的价值:突出在数据分析与挖掘
python·数据分析
即兴小索奇4 小时前
Codeium:免费开源代码自动补全工具,高效管理代码片段告别开发卡壳
ai·商业·ai商业洞察·即兴小索奇
我没想到原来他们都是一堆坏人4 小时前
通过Gen AI SDK调用gemini 2.5 pro,单独上传pdf文件 | ai agent 开发笔记 2025.9.2 Day 2
ai·google·pdf·sdk·gemini
ITZHIHONH4 小时前
FastGPT源码解析 工作流、知识库、大模型、Agent等核心代码文件梳理
ai·ai编程