Forums

Cannot deploy REST API app

I have a REST API that I want to deploy on pythonanywhere. I'm using Flask for the app. It uses an API key I am trying to hide in a .env file. I get this error message:

2022-10-31 05:09:53,356: Error running WSGI application
2022-10-31 05:09:53,356: ModuleNotFoundError: No module named 'dotenv'
2022-10-31 05:09:53,356:   File "/var/www/vbosu_pythonanywhere_com_wsgi.py", line 10, in <module>
2022-10-31 05:09:53,357:     from dotenv import load_dotenv

I attempted to use the only guidance I can find on this, from here and here. Nothing is working. Here is the code in my wsgi.py file:

import sys import os from dotenv import load_dotenv

project_folder = os.path.expanduser('~/home/vbosu/mysite') load_dotenv(os.path.join(project_folder, '.env'))

project_home = '/home/vbosu/mysite' if project_home not in sys.path: sys.path.insert(0, project_home)

ENDPOINT = os.getenv("ENDPOINT") APP_ID = os.getenv("APP_ID")

from app import app as application # noqa

Does anyone have any idea what I'm missing here?

Do you have python-dotenv package installed there?

Where is "there", exactly? I wrote the app with PyCharm; the python-dotenv package is installed in the PyCharm project folder. The program works with localhost.

When you run your code on PythonAnywhere, then you'll need to install the packages that it depends on into your account. I see that your website is set up to use Python 3.10, with no virtualenv, so you'll need to run the bash command

pip3.10 install --user python-dotenv

...to install the package. See this help page for more details.