Forums

No opneai library

I tried to follow an openai with python tutorial and got an error (see below):

ModuleNotFoundError: No module named 'openai'

So my questions are:

  1. Does the Python Anywhere platform have the openai library?
  2. If not, is there an equivalent method to a "pip openai" command to add it to my project?

Take a look at https://help.pythonanywhere.com/pages/InstallingNewModules

I'll check this out and tell you how it goes, thanks!

Openai with Python Anywhere update.

Source code here...

from flask import Flask
import openai

app = Flask(__name__)


@app.route("/", methods=["GET", "POST"])
def chatbot():
    # Set up the OpenAI API client
    openai.api_key = "***************************************"

    # Set up the model and prompt
    model_engine = "text-davinci-003"
    prompt = "Hello, how are you today?"

    # Generate a response
    completion = openai.Completion.create(
        engine=model_engine,
        prompt=prompt,
        max_tokens=1024,
        n=1,
        stop=None,
        temperature=0.5,
    )

    response = completion.choices[0].text
print(response)

Bash

Well I started a Bash console and the made a virtual environment with Python 10.

$ mkvirtualenv my-virtualenv --python=python3.10

Then I did a pip install openai.

Successfully installed aiohttp-3.8.4 aiosignal-1.3.1 async-timeout-4.0.2 attrs-23.1.0 certifi-2022.12.7 charset-normalizer-3.1.0 frozenlist-1.3.3 idna-3.4 multidict-6.0.4 openai-0.27.6 requests-2.2

Then I ran the command:

python path/to/mysite/flask_app.py

and it came back with no errors

Code Editor

I left the bash console and went back to the source code and refreshed/ran the code again and I am still getting an error posted below.

import openai
ModuleNotFoundError: No module named 'openai'

Any Thoughts?

It doesn't look like you've configured your website to use the virtualenv that you created; there's an option to do that on the "Web" page.

Thank you for responding. I went to the "Web" page then to the section entitled "Virtualenv" the entered the path to my virtual environment.

/home/*****/.virtualenvs/my-virtualenv/

then I went back to the code editor and reloaded/ran the program and I'm still getting the same error:

import openai
ModuleNotFoundError: No module named 'openai'

All suggestions are welcomed. Thank you in advance.

Looks like you've definitely installed the openai module somewhere. The thing to be sure about is making sure that you're installing the requirments and running the code in the same virtual environment.

In the console, when you're installing the requirements and running code, make sure that the virtualenv is activated - workon my-virtualenv

On the webapp page, make sure the app is configured to use the webapp (Looks like you've done this).