pythonvisual-studio-codeinterpreter

ModuleNotFoundError: No Module Named openai


import requests
from bs4 import BeautifulSoup
import openai 

#write each line of nuclear.txt to a list
with open('nuclear.txt', 'r') as f:
    lines = f.readlines()

#remove the newline character from each line
lines = [line.rstrip() for line in lines]

#gather the text from each website and add it to a new txt file
for line in lines:
    r = requests.get(line)
    soup = BeautifulSoup(r.text, 'html.parser')
    text = soup.get_text()
    with open('nuclear_text.txt', 'a') as f:
        f.write(text)

I'm trying to import openai, however it keeps throwing the error module not found. I have done pip install openai and it downloads, but it appears to be the wrong version of python. How do I select the correct one for pip to install to? I am using VSCode

pip install openai


Solution

  • Follow the steps below to install the openai package for the current interpreter
    1. run the following code

      import sys
      print(sys.executable)
      
    2. get the current interpreter path

      enter image description here

    3. Copy the path and install openai using the following command in the terminal

      C:\WorkSpace\pytest10\.venv\Scripts\python.exe -m pip install openai
      

      Modify the path in the above command to the interpreter path you got.

      enter image description here