ModuleNotFoundError: No Module Named openai

Question:

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

Asked By: ArcheausGalacto

||

Answers:

Try using pip3 install openai, as it installs openai for python3, not python2 (if you have it installed). If you only have python3, pip and pip3 are basically the same thing (I think).

Answered By: Nic13Gamer
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:WorkSpacepytest10.venvScriptspython.exe -m pip install openai
    

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

    enter image description here

Answered By: JialeDu

The answer of JialeDu worked for me, but after I added the right path to my pip setting in the Environment Variables from windows

If you’d rather run pip (or other tools) from any location, you’ll need to add the directory in which it’s installed as a PATH environment variable by doing the following:

1 – Open up the Control Panel and navigate to System and Security > System
2 -Click on the Advanced system settings link on the left panel
3 -Click Environment Variables.
4 -Under System Variables, double-click the variable PATH.
5 -Click New, and add the directory where pip is installed, e.g. C:Python33Scripts, and select OK.

Answered By: Patrick Ribeiro

I had the same issue, when using the embeddings library from openAI. I kept adding the missing modules, and it worked until sklearn. What worked for me was:

pip install scikit-learn
Answered By: harcipulyka