0

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

dancingbush
  • 2,131
  • 5
  • 30
  • 66
  • 1
    HOW have you installed openai and gradio? Remember that your system has multiple installations of Python. If your Code is using its own installation, then you'll need to install the modules there, through Code. – Tim Roberts May 27 '23 at 18:35
  • I installed openai and radio using pip, when i type 'where python' the installation are: /opt/anaconda3/bin/python /usr/local/bin/python /usr/bin/python – dancingbush May 28 '23 at 12:41
  • Also tried excuting with python / python3 intreperters- same result (python script.py and python3 script.py in terminal), also tried py -3.8 but py doesn't appear to be available for MacOS – dancingbush May 28 '23 at 12:46
  • When I type in abs path for python3 it executes, but throws the exception in terminal : /usr/local/bin/python3 CiaranAIBot.py Traceback (most recent call last): File "/Users/callanmooneys/Desktop/HDip software Dev/AI Open ChatGP Apps/CiaranAIBot.py", line 1, in import openai ModuleNotFoundError: No module named 'openai' – dancingbush May 28 '23 at 12:48
  • Furthermore- i just installed Python 3.10.11, but when i do a version check, its still returning the older version: python --version Python 3.9.12, all v confusing – dancingbush May 28 '23 at 12:52
  • 1
    Right, because VSCode has its own Python installation. You CAN configure VSCode to use your preferred installation, or you can install the modules inside of VSCode. – Tim Roberts May 28 '23 at 18:08
  • Got it now, openai and gradio were installed with python 3.9, VSC was using 3.11 installation by default, so i installed same packages using pip3.11 , and it works now, thanks for your help – dancingbush May 28 '23 at 21:32

1 Answers1

1

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)

balbok
  • 394
  • 3
  • 14
  • Thanks - that was it, VSC was running 3.11, packages openai and gradio were installed against 3.9 when i ran pip3 install.... so I just installed against 3.11 (pip3.11 install openai ) – dancingbush May 28 '23 at 21:33