Forums

Flask app import error.

I am having some difficulties getting a Flask app to run on PythonAnywhere. I am a newcomer to both Flask and PythonAnywhere (reasonably competent with Python), so I know I have something messed up somewhere, but I've been notably unsuccessful in figuring out what I've got wrong.

I have already checked the page about import errors but wasn't able to find an answer there. I've made every change I can think of but continue to get error messages about this. Any and all help will be greatly appreciated!

The error log shows:

 28: Error running WSGI application
 ModuleNotFoundError: No module named 'app'
    File "/var/www/mandrequip_pythonanywhere_com_wsgi.py", line 16, in <module>
        from flask_app import app as application  # noqa

        File "/home/mandrequip/mysite/flask_app.py", line 8, in <module>
            from app import routes, models

Assume the account name is "acctName." (It's a free account.) The structure of acctName is:

/home/acctName/
    |--> .cache/
    |--> .local/
    |--> .virtualenvs/
    |--> mysite/
            |--> flask_app.py
            |--> models.py
            |--> routes.py
            |--> config.py
            |--> parts.sqlite
            |--> templates/
                |--> index.html
                |--> base.html
            |--> static/
                |--> css
                    |--> app.css

    |--> venv/

flask_app.py:

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

app = Flask(__name__)

from app import routes, models

/var/www/acctName_pythonanywhere_com_wsgi.py:

import sys

# add your project directory to the sys.path
project_home = '/home/acctName/mysite'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

from flask_app import app as application  # noqa

If I put all the routes into the flask_app.py file, the app runs, but with the structure and python shown above, it fails. On my local development Mac, it runs with no problem at all. I think this is a problem with that last line of the WSGI.py file, but I cannot figure out how to correct it.

from app import routes, models is wrong, as there is no app module to import from. Looks like you need to directly import from routes and models in your flask_app.py.

Thank you! That fixed up that problem, which led me to another and another, but I got those corrected. Now to sort out database access and I'll be good. Thanks very much for your help.