无法解决在使用Visual Studio Code时的Python导入异常。

huangapple go评论116阅读模式
英文:

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.7

pip install gradio

这是脚本:

  1. import openai
  2. import gradio as gr
  3. print("Debugging AI script")
  4. openai.api_key = "..."
  5. print("API Key: " + openai.api_key)
  6. messages = [
  7. {
  8. "role": "system",
  9. "content": "You are a helpful and kind AI Assitant"
  10. },
  11. ]
  12. def chatbot(input):
  13. if input:
  14. messages.append({
  15. "role": "user",
  16. "content": input
  17. })
  18. chat = openai.ChatCompletion.create(model="gpt-3.5-turbo", messages=messages)
  19. reply = chat.choices[0].message.content
  20. messages.append({
  21. "role": "assistant",
  22. "content": reply
  23. })
  24. return reply
  25. inputs = gr.inputs.Textbox(lines=7, label="Chat with Mega Brain")
  26. outputs = gr.outputs.Textbox(label="Speak Sir")
  27. 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:

  1. import openai
  2. import gradio as gr
  3. print("Debugging AI script")
  4. openai.api_key = "..."
  5. print("API Key: " + openai.api_key)
  6. messages = [
  7. {
  8. "role": "system",
  9. "content":"You are a helpful andd kind AI Assitant"
  10. },
  11. ]
  12. def chatbot(input):
  13. if input:
  14. messages.append({
  15. "role":"user",
  16. "content": input
  17. })
  18. chat = openai.ChatCompletion.create(model="gpt-3.5-turbo",
  19. messages = messages)
  20. reply = chat.choices[0].message.content
  21. messages.append({
  22. "role":"assistant",
  23. "content": reply
  24. })
  25. return reply
  26. inputs = gr.inputs.Textbox(lines=7,
  27. label="Chat with Mega Brain")
  28. outputs = gr.outputs.Textbox(label="Speak Sir")
  29. gr.Interface(fn=chatbot, inputs=inputs,
  30. outputs=outputs, title="AI Mega-Brain Mega-Chat",
  31. description="Ask the Brain anything",
  32. 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,根据您的设置而定)。

然后按照这些说明选择与上述命令输出匹配的路径。

最后,我强烈建议设置每个项目的环境(参见:MambaCondaPoetry都以稍微不同的方式实现类似的目标)。

英文:

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)

huangapple
  • 本文由 发表于 2023年5月28日 02:34:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76348441.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定