Forums

stuck debugging "no module named ..."

Thanks for reading this. My error log shows this:

2023-01-31 23:02:23,190: Error running WSGI application
2023-01-31 23:02:23,190: ModuleNotFoundError: No module named 'openai'
2023-01-31 23:02:23,190:   File "/var/www/studiozandra_pythonanywhere_com_wsgi.py", line 16, in <module>
2023-01-31 23:02:23,191:     from flask_app import app as application  # noqa
2023-01-31 23:02:23,191: 
2023-01-31 23:02:23,191:   File "/home/studiozandra/mysite/flask_app.py", line 5, in <module>
2023-01-31 23:02:23,191:     import web_parse
2023-01-31 23:02:23,191: 
2023-01-31 23:02:23,192:   File "/home/studiozandra/mysite/web_parse.py", line 1, in <module>
2023-01-31 23:02:23,192:     import openai
2023-01-31 23:02:23,192: ***************************************************
2023-01-31 23:02:23,192: If you're seeing an import error and don't know why,
2023-01-31 23:02:23,193: we have a dedicated help page to help you debug: 
2023-01-31 23:02:23,193: https://help.pythonanywhere.com/pages/DebuggingImportError/
2023-01-31 23:02:23,193: ***************************************************

I followed the instructions in the error log to consult https://help.pythonanywhere.com/pages/DebuggingImportError/

I was able to run the wysgi file, but I got confused at the suggestion stating, "Try doing the import manually at the command line. Then, check whether they really are coming from where you think they are."

Requirement already satisfied: frozenlist>=1.1.1 in /home/studiozandra/.virtualenvs/my-virtualenv

/lib/python3.8/site-packages (from aiohttp->openai) (1.3.3)
Requirement already satisfied: aiosignal>=1.1.2 in /home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages (from aiohttp->openai) (1.3.1)
Requirement already satisfied: yarl<2.0,>=1.0 in /home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages (from aiohttp->openai) (1.8.2)
Requirement already satisfied: attrs>=17.3.0 in /home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages (from aiohttp->openai) (22.2.0)
(my-virtualenv) 23:02 ~/mysite (master)$ python import openai
python: can't open file 'import': [Errno 2] No such file or directory
(my-virtualenv) 05:45 ~/mysite (master)$ import openai                                                                                                                 
import-im6.q16: unable to open X server `' @ error/import.c/ImportImageCommand/359.
(my-virtualenv) 05:45 ~/mysite (master)$ python -i /var/www/studiozandra_pythonanywhere_com_wsgi.py
>>> import openai
>>> print(openai.__file__)
/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/__init__.py

My problem is I don't really understand the file structure/where things are supposed to be vs. where they actually are. I know it's not good to stay stuck for days so decided to ask for assistance.

You do not have openai installed in the Python version/virtualenv that you are using for your web app. See https://help.pythonanywhere.com/pages/InstallingNewModules/

Thanks for replying, Glenn. I did reinstall requirements.txt, if you'll note where it says

Requirement already satisfied: frozenlist>=1.1.1 in /home/studiozandra/.virtualenvs/my-virtualenv

Are you saying I need to install it somewhere else, different directory? I'm a bit lost as I've been fixing this site for days.

Are you sure that you are running the app in that venv?

fjl -- no, I am not sure: how can I confirm this?

You can see the virtualenv on the "Web" page inside PythonAnywhere.

It might also be worth checking that you're looking at the right error message in the log file -- the file isn't cleared when you reload the site, so the most recent error will always be the one at the bottom of the file.

Thanks, giles.

I deleted all venvs and created a new one, this time ensuring that my sys path has the correct site-packages directory.

At least that got me some new import errors, and I had to adjust some package versions. New error is

ImportError: cannot import name 'asynccontextmanager'

This has to do with OpenAI's package (I think) so I am trying to downgrade version by version to see if that helps. I've also once again created a new venv with the Python version mention in OpenAI's Github page, although it does state 3.7.1+, so that shouldn't be an issue, but I tried it anyway.

If no version works I will have to check the OpenAI forums, I guess.

The full error:

2023-02-03 14:31:25,624: Error running WSGI application
2023-02-03 14:31:25,640: ImportError: cannot import name 'asynccontextmanager'
2023-02-03 14:31:25,640:   File "/var/www/studiozandra_pythonanywhere_com_wsgi.py", line 19, in <module>
2023-02-03 14:31:25,640:     from flask_app import app as application  # noqa
2023-02-03 14:31:25,641: 
2023-02-03 14:31:25,641:   File "/home/studiozandra/mysite/flask_app.py", line 5, in <module>
2023-02-03 14:31:25,641:     import web_parse
2023-02-03 14:31:25,641: 
2023-02-03 14:31:25,642:   File "/home/studiozandra/mysite/web_parse.py", line 4, in <module>
2023-02-03 14:31:25,642:     import openai
2023-02-03 14:31:25,642: 
2023-02-03 14:31:25,642:   File "/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/__init__.py", line 9, in <module>
2023-02-03 14:31:25,643:     from openai.api_resources import (
2023-02-03 14:31:25,643: 
2023-02-03 14:31:25,643:   File "/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/api_resources/__init__.py", line 1, in <module>
2023-02-03 14:31:25,643:     from openai.api_resources.completion import Completion  # noqa: F401
2023-02-03 14:31:25,643: 
2023-02-03 14:31:25,643:   File "/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/api_resources/completion.py", line 4, in <module>
2023-02-03 14:31:25,643:     from openai.api_resources.abstract import DeletableAPIResource, ListableAPIResource
2023-02-03 14:31:25,644: 
2023-02-03 14:31:25,644:   File "/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/api_resources/abstract/__init__.py", line 3, in <module>
2023-02-03 14:31:25,644:     from openai.api_resources.abstract.api_resource import APIResource
2023-02-03 14:31:25,645: 
2023-02-03 14:31:25,645:   File "/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/api_resources/abstract/api_resource.py", line 4, in <module>
2023-02-03 14:31:25,645:     from openai import api_requestor, error, util
2023-02-03 14:31:25,645: 
2023-02-03 14:31:25,645:   File "/home/studiozandra/.virtualenvs/my-virtualenv/lib/python3.8/site-packages/openai/api_requestor.py", line 7, in <module>
2023-02-03 14:31:25,645:     from contextlib import asynccontextmanager

Make sure you are using a version of Python that has asynccontextmanager: https://docs.python.org/3/library/contextlib.html#contextlib.asynccontextmanager

yes, it's 3.7.1

You could try updating your system image. You'd have to rebuild the virtualenv, but I have no trouble with "from contextlib import asynccontextmanager" on the latest system image: https://help.pythonanywhere.com/pages/ChangingSystemImage/

thank you that did it! (of course now I have a new error, but at least the site loads LOL) thanks all.