Forums

Problem getting WSGI file working

Hi Folks, I know this isn't the ideal sub for this question; it's actually my second choice but r/pythonanywhere is a really small community, so I'm trying again here.

I'm having a bit of trouble deploying a flask/react project. I think it's just faulty syntax but it could be deeper.

my project is set up like this ->

PROJECTNAME
|-/frontend (react)
|-/backend (flask)
    |-__init.py__
    |-/app
        |- __init__.py

The Flask app code is in /backend/app/init.py

so to start from cli is simply python -m backend.app

I've got this code in the Flask area of the .._wsgi.py ->

import sys

path = "/home/{userName}/{projectName}"
if path not in sys.path:
    sys.path.append(path)

from .backend.app import app as application

app is defined in backend.app.init.py as -> app = Flask(__name__)

This is not working. The app takes ages loading and comes back with the "Something Went wrong" page and this error

Error running WSGI application 2022-05-21 18:09:47,226: ImportError: attempted relative import with no known parent package 2022-05-21 18:09:47,227: File "/var/www/technodiver_pythonanywhere_com_wsgi.py", line 112, in <module> 2022-05-21 18:09:47,227: from .backend.app import app as application

This is my first time deploying a completed app to pythonAnywhere. In local dev I have to turn on the both the react frontend: npm run start and also the flask backend: python -m backend.app

Can someone give me some help configuring this, please? I've followed the site instructions for flask projects, obviously didn't work out and I'm not sure where to go or what do to next. Thanks

Don't use a relative import (from .backend.app import app as application). If you do not know well how Python handles relative imports, you'll have issues with those that are difficult to debug. Use from backend.app import app as application