Forums

NameError: name 'openai' is not defined

I have the following code in my flask_app.py file:

import openai

from flask import Flask, render_template, request, session import
flask import requests

app = Flask(__name__)

openai.api_key = <my API key>

When I try to run my webapp, I get the following in the error log:

Error running WSGI application 2023-08-07 09:10:59,998: NameError:
name 'openai' is not defined 2023-08-07 09:10:59,998:   File
"/var/www/mariaia_pythonanywhere_com_wsgi.py", line 16, in <module>
2023-08-07 09:10:59,998:     from flask_app import app as application 
# noqa 2023-08-07 09:10:59,999:  2023-08-07 09:10:59,999:   File "/home/mariaia/mysite/flask_app.py", line 2, in     module> 2023-08-07
09:10:59,999:     openai.api_key = <my API key>

Any suggestions on how I can fix this?

I think you just need to install the openai module for the version of Python that you are using for your website -- eg. pip3.10 install openai if you're using Python 3.10.

Thanks. I tried that, but it did not work.

I ran:

18:22 ~ $ python --version
Python 3.10.5
04:09 ~ $ pip3.10.5 install openai
bash: pip3.10.5: command not found

04:10 ~ $ pip3.10 install openai
Defaulting to user installation because normal site-packages is not writeable
Looking in links: /usr/share/pip-wheels
Requirement already satisfied: openai in ./.local/lib/python3.10/site-packages (0.27.8)
Requirement already satisfied: requests>=2.20 in /usr/local/lib/python3.10/site-packages (from openai) (2.28.1)
Requirement already satisfied: tqdm in /usr/local/lib/python3.10/site-packages (from openai) (4.62.3)
Requirement already satisfied: aiohttp in ./.local/lib/python3.10/site-packages (from openai) (3.8.5)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /usr/local/lib/python3.10/site-packages (from requests>=2.20->openai) (1.26.9)
Requirement already satisfied: idna<4,>=2.5 in /usr/local/lib/python3.10/site-packages (from requests>=2.20->openai) (3.3)
Requirement already satisfied: charset-normalizer<3,>=2 in /usr/local/lib/python3.10/site-packages (from requests>=2.20->openai) (2.1.0)
Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.10/site-packages (from requests>=2.20->openai) (2022.6.15)
Requirement already satisfied: frozenlist>=1.1.1 in ./.local/lib/python3.10/site-packages (from aiohttp->openai) (1.4.0)
Requirement already satisfied: attrs>=17.3.0 in /usr/local/lib/python3.10/site-packages (from aiohttp->openai) (21.4.0)
Requirement already satisfied: aiosignal>=1.1.2 in ./.local/lib/python3.10/site-packages (from aiohttp->openai) (1.3.1)
Requirement already satisfied: multidict<7.0,>=4.5 in ./.local/lib/python3.10/site-packages (from aiohttp->openai) (6.0.4)
Requirement already satisfied: async-timeout<5.0,>=4.0.0a3 in /usr/local/lib/python3.10/site-packages (from aiohttp->openai) (4.0.2)
Requirement already satisfied: yarl<2.0,>=1.0 in ./.local/lib/python3.10/site-packages (from aiohttp->openai) (1.9.2)

And got the same error message.

I'm running python 3.10.5.

Could you try printing "sys.path" in that file? Then you can see where it looks for modules, and check that openai is installed in the right location.

Thank you.

In my flask_app.py (Python3.10) console:

>>> import sys
>>> print(sys.path)
['.', '/bin', '/usr/local/lib/python310.zip', '/usr/local/lib/python3.10', '/usr/local/lib/python3.10/lib-dynload', '/home/mariaia/.local/lib/python3.10/site-packages', '/usr/local/lib/python3.10/site-packages']

Does that give you the info you need? (Sorry, I'm a linux newbie)

Did you try to execute import openai in that console?

Thanks.

I just ran

import openai

In my Python3.10 console.

I'm not seeing any errors in my error.log now, but when I try to open the web page, http://mariaia.pythonanywhere.com/ I'm still getting a 404 Not Found error.

Actually, I think the 404 error is because of other issues in my setup, so this item is resolved.

Thank you for your help!

Glad you figured that out!