英文:
Cant resolve Import exception in python using Visual Studio Code
问题
以下是您要翻译的内容:
我在尝试在Visual Studio Code(VSC)中执行Python脚本时遇到了以下异常。我怀疑这是一个简单的环境配置问题,但我对Python还不熟悉,看不出问题所在。
Pylance无法解析“openai”的导入
Pylance无法解析“gradio”的导入
我正在使用Mac Catalina 10.15.7。
VSC版本:1.75.1。
我已经安装了Python、openai和gradio:
Python 3.9.12(基础)
openai 0.27.7pip install gradio
这是脚本:
import openai
import gradio as gr
print("Debugging AI script")
openai.api_key = "..."
print("API Key: " + openai.api_key)
messages = [
{
"role": "system",
"content": "You are a helpful and kind AI Assitant"
},
]
def chatbot(input):
if input:
messages.append({
"role": "user",
"content": input
})
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
reply = chat.choices[0].message.content
messages.append({
"role": "assistant",
"content": reply
})
return reply
inputs = gr.inputs.Textbox(lines=7, label="Chat with Mega Brain")
outputs = gr.outputs.Textbox(label="Speak Sir")
gr.Interface(fn=chatbot, inputs=inputs, outputs=outputs, title="AI Mega-Brain Mega-Chat", description="Ask the Brain anything", theme="compact").launch(share=True)
我尝试了许多解决方案,包括这里发布的解决方案,但都没有成功。
感谢任何帮助。
英文:
I am getting following exceptions when trying to execute a Python script in Visual Studio Code (VSC).
I suspect this is a simple env config issue but am new to Python and can't see it.
> Import "openai" could not be resolved by Pylance
> Import "gradio" could not be resolved by Pylance
I am using Mac Catalina 10.15.7.
VSC Version: 1.75.1.
I have installed Python, openai and gradio:
> --version Python 3.9.12 (base)
--version openai 0.27.7
>
> pip install gradio
This is the script:
import openai
import gradio as gr
print("Debugging AI script")
openai.api_key = "..."
print("API Key: " + openai.api_key)
messages = [
{
"role": "system",
"content":"You are a helpful andd kind AI Assitant"
},
]
def chatbot(input):
if input:
messages.append({
"role":"user",
"content": input
})
chat = openai.ChatCompletion.create(model="gpt-3.5-turbo",
messages = messages)
reply = chat.choices[0].message.content
messages.append({
"role":"assistant",
"content": reply
})
return reply
inputs = gr.inputs.Textbox(lines=7,
label="Chat with Mega Brain")
outputs = gr.outputs.Textbox(label="Speak Sir")
gr.Interface(fn=chatbot, inputs=inputs,
outputs=outputs, title="AI Mega-Brain Mega-Chat",
description="Ask the Brain anything",
theme="compact").launch(share=True)
Ive tried a number of solutions, included these posted here, to no avail.
Any help appreciated.
thanks
答案1
得分: 1
确保VS Code中使用的解释器与您在终端中使用的解释器相匹配。在您安装Python的环境中,键入以下命令:which python
(或which python3
,根据您的设置而定)。
然后按照这些说明选择与上述命令输出匹配的路径。
最后,我强烈建议设置每个项目的环境(参见:Mamba,Conda或Poetry都以稍微不同的方式实现类似的目标)。
英文:
You need to make sure that interpreter used by VS Code matches one you are using in the terminal. In the environment you installed python in type: which python
(or which python3
, depending on your setup.
Then follow these instructions and select the path that matches output of the command above.
Lastly, I highly recommend setting up per-project environments (see: Mamba, Conda, or Poetry all accomplishing the similar goals in slightly different ways)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论