没有名为Openai的模块

发布于 2025-01-21 12:19:39 字数 402 浏览 0 评论 0原文

我已经使用pip安装OpenAI在笔记本电脑上安装了OpenAI。

已安装在我的笔记本电脑上,并在我的代码文件所在的同一文件夹上安装后。但是,当我尝试运行代码时,我会得到ImporterRor:no模块名为OpenAI

这是文件中的代码。非常简单:

import openai

openai.api_key = API_KEY

prompt = "Say this is a test"

response = openai.Completion.create(
    engine="text-davinci-001", prompt=prompt, max_tokens=6
)

print(response)

我在做什么错?

I've installed openai on my laptop with pip install openai.

Have installed on my laptop and after installed on the same folder where my code file is. But when I try to run the code I get ImportError: No module named openai

This is the code from the file. Pretty simple:

import openai

openai.api_key = API_KEY

prompt = "Say this is a test"

response = openai.Completion.create(
    engine="text-davinci-001", prompt=prompt, max_tokens=6
)

print(response)

What am I doing wrong?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(16

寻梦旅人 2025-01-28 12:19:39

我遇到了同样的问题,而我所做的一切都是:

首先将OpenAi软件包卸载:

pip uninstall openai

然后我升级了PIP:

pip install --upgrade pip

然后,我将OpenAPI软件包重新安装

pip install openai

并使用。

I encountred the same problem and all what I did was:

First uninstall the openai package with :

pip uninstall openai

Then I upgraded pip with :

pip install --upgrade pip

And i re-installed the openapi package with

pip install openai

And it worked.

微凉徒眸意 2025-01-28 12:19:39

如果您有多个版本的Python

可以显示PIP已安装OpenAI软件包的位置,则可以运行此命令,

pip show openai

您将在您看到这样的输出中

Name: openai
Version: 0.26.4
Summary: Python client library for the OpenAI API
Home-page: https://github.com/openai/openai-python
Author: OpenAI
Author-email: [email protected]
License: None
Location: /home/${USER}/.local/lib/python3.8/site-packages
Requires: requests, tqdm, aiohttp
Required-by: 

,pip安装python版本3.8的openai。

因此,如果默认的Python版本为2.7,则在运行Python时进行 import openai ,这将行不通。

您可以将默认的Python版本更改为软件包OpenAI的同一方面,

sudo update-alternatives --config python

然后选择正确的版本(我为3.8)。

您也可以尝试为默认Python版本安装OpenAI:

python -m pip install openai

This can happen if you have multiple versions of python

to show where pip has installed openai package, you can run this command

pip show openai

you will have an output like this

Name: openai
Version: 0.26.4
Summary: Python client library for the OpenAI API
Home-page: https://github.com/openai/openai-python
Author: OpenAI
Author-email: [email protected]
License: None
Location: /home/${USER}/.local/lib/python3.8/site-packages
Requires: requests, tqdm, aiohttp
Required-by: 

as you see, for me pip installs the package openai for the python version 3.8.

so if the default python version is 2.7 for example, when running python then making import openai, this will not work.

you can change the default python version to the same verion of the package openai, use

sudo update-alternatives --config python

Then select the correct version (3.8 for me).

you can also try to install openai for your default python version:

python -m pip install openai
怎言笑 2025-01-28 12:19:39

最佳答案对我不起作用,但这确实是:

我在Mac上使用VS代码。我必须选择正确的Python解释器。我使用Python 3和PIP3代替PIP。

使用以下方式卸载OpenAI软件包

pip uninstall openai

(请务必使用pip3):

pip3 install --upgrade pip

我将OpenAPI软件包重新安装(请务必使用pip3):

pip3 install openai

Top answer didn't work for me, but this did:

I am using VS code on a mac. I had to select the correct Python interpreter. I am using Python 3 and pip3 instead of pip.

Uninstall the openai package with :

pip uninstall openai

Upgraded pip with (be sure to use pip3):

pip3 install --upgrade pip

And i re-installed the openapi package with (be sure to use pip3):

pip3 install openai
神也荒唐 2025-01-28 12:19:39

运行以下命令后,

pip install openai

如果使用Visual Studio代码重新启动内核。它对我有用。

After you run the following command

pip install openai

If you are using visual studio code restart your kernal.it worked for me.

enter image description here

厌倦 2025-01-28 12:19:39

对于试图在MacOS上运行它的人(请安装 flask 首先),然后使用

sudo flask run

并有效。

For the one who tries to run it on macOS (Please install Flask first), then use

sudo flask run

and it works.

三五鸿雁 2025-01-28 12:19:39

尝试它对我有用,替换3.10的Python版本

sudo pip3.10 install openai

Try it works for me , replace 3.10 for your python version

sudo pip3.10 install openai
谁与争疯 2025-01-28 12:19:39

尝试在摘要之后放置- 用户

pip install openai --user

此后,错误不会显示&该代码对我来说很好。

Try putting --user after the snippet.

pip install openai --user

After this, the error doesn't show up & the code works fine for me.

烟花易冷人易散 2025-01-28 12:19:39

我试图通过安装Python3的Mac上的VS代码来运行我的OpenAi Python脚本。当我尝试通过按VS代码的Pythance Python扩展程序提供的播放按钮来运行脚本,我一直在获取错误消息no模块名为OpenAI

对我有帮助的是,使用标准提示> PIP安装OpenAi OpenAI ,然后通过终端提示Python3 Script.py执行我的脚本。

I was trying to run my openai python script through VS Code on a Mac with python3 installed. When I tried to run my script by pressing the play button supplied by VS Code's Pylance Python extension I kept getting the error message No module named openai.

What helped me was, to install openai with the standard prompt pip install openai and by executing my script through the terminal prompt python3 script.py.

鸠魁 2025-01-28 12:19:39

如果您以admin(或sudo)为adman脚本运行python脚本,则会引发错误Importerror:no模块名为OpenAI。但是使用admin(或sudo),它的运行良好

in case you are running the python script as admin (or sudo) it throws the error ImportError: No module named openai. but with out admin (or sudo) it just runs fine

沫离伤花 2025-01-28 12:19:39

这可能是临时VS代码错误。尝试关闭应用程序并再次尝试。它对我在Pycharm上效果很好。

我用了这个:

pip3 install openai

This might be a temporary VS code error. Try closing the app and trying it again. It worked well for me on pycharm.

I used this:

pip3 install openai
寄居人 2025-01-28 12:19:39

如果有人在模拟器上运行 firebase 时遇到了这个问题:

您必须确保在pyvenv.cfg上设置此配置为'true':

include-system-site-packages = true

If anyone runs into this problem when running Firebase on an emulator:

You have to make sure this config is set to 'true' on pyvenv.cfg:

include-system-site-packages = true
捂风挽笑 2025-01-28 12:19:39

我确实在根部的MacOS终端上安装了PIP安装OpenAI,然后该项目随后进行了。

I did pip install openai on the macOS terminal at the root and the project worked afterwards.

沫雨熙 2025-01-28 12:19:39

在您的IDE上,将OpenAI模块手动添加到解释器中。
问题将得到解决!

对于Pycharm,

请转到文件 - >设置 - >项目 - > Python解释器 - >添加模块(寻找 +符号)
tadaa!

On your IDE , manually add the openai module to the interpreter .
The issue will be resolved !

For Pycharm ,

Go to File -> settings -> project -> python interpreter -> add the module (look for + symbol )
Tadaa !

荒路情人 2025-01-28 12:19:39

在使用Pycharm的情况下,面对这个问题,PyCharm为每个项目都保留了单独的执行环境。

我尝试在VSCODE中运行相同的代码,它没有任何错误。

Was facing this issue with using pyCharm, turned out pyCharm maintains separate execution environment for each of the projects.

I tried to run the same code in VSCode, it ran without any error.

全部不再 2025-01-28 12:19:39

就我而言,我再次卸载并安装了OpenAI,但是,我仍然遇到同样的错误。
我发现我的机器上安装了2个Python版本。因此

python main.py

给了我一个错误

python3 main.py

,使用该问题 。请检查您的情况是否也是问题。注意:我使用的是虚拟env,但是我仍然遇到此错误。

In my case I uninstalled and installed openai again, however, I was still getting the same error.
I found out that I had 2 python versions installed on my machine. So executing

python main.py

Gave me an error, however, using

python3 main.py

Solved the issue. Please check if this is the problem in your case too. Note: I was using a virtual env but I was still getting this error.

月寒剑心 2025-01-28 12:19:39

如果您的Jupyter Lab/Notebook正在运行全部关闭,请关闭终端,通过PIP安装模块并重新启动Jupyter。

因为在Jupyter从另一个终端会话运行时安装模块对我不起作用。

If you have Jupyter lab/notebook running just close it all, shut down the terminal, install the module via pip and restart Jupyter.

Because installing the module while Jupyter was running from another terminal session didn't work for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文