Forums

Flask app with factory in __init__.py ModuleNotFoundError

I built a website using the flask application factory method but I am having trouble deploying it since I keep getting 'ModuleNotFoundError' in my error logs. My directory structure is pretty simple:

  • mysite/
  • Website/ (the python package)
    • main.py
    • init.py
    • views.py
    • auth.py

my main.py file:

from website import create_app
app = create_app()
if __name__ == '__main__':
    app.run()

my init.py file:

from flask import Flask
    def create_app():
        app = Flask(__name__)
        app.config['SECRERT_KEY'] = '1b2l9d5ti8bffhf'

        from .auth import auth
        from .views import views

        app.register_blueprint(views, url_prefix='/')
        app.register_blueprint(auth, url_prefix='/')

        return app

my wsgi.py file:

import sys

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

#import flask app but need to call it "application" for WSGI to work
from website import create_app
application = create_app()  # noqa

I don't know what I am doing wrong but my error logs keep saying:

Error running WSGI application

ModuleNotFoundError: No module named 'website'

File "/var/www/mp08100_pythonanywhere_com_wsgi.py", line 16, in <module>

from main import app as application # noqa File "/home/Mp08100/mysite/main.py", line 1, in <module> from website import my_site

There is no module named website in the project that you described above. It's case sensitive.

I am new to this so I am a little confused but I thought having init.py in my directory would make the folder called website a module. Since this doesn't seem to be the case, what would I need to change to get this to work? It seems like I simply need to replace website with the right name in my WSGI file but I don't know what that right name is. The site works fine on a local server therefore website must be a module since from website import create_app doesn't create an error in my main.py file. I guess I don't understand why it doesn't work the same way on here even though the directory structure is the same.

Take a look at https://help.pythonanywhere.com/pages/DebuggingImportError/

Unfortunately, I already did but I wasn't able to come up with a solution that worked. My approach to the application factory seems fairly standard so I don't know what I could do differently to get this to work. It seems like the first line in main.py is the issue but like I said it works fine on a local server. I took a lot of inspiration from this video https://youtu.be/dam0GPOAvVI . In which Tim explains that it is possible to use such a statement since the init.py file makes the folder in which the files are a python package.

What errors exactly are you getting, @Mp000000?

The only error I get is the one I described in my original post, the 'module not found' error. It appears that my website folder isn't recognized as a module, or at least that is how I am interpreting this.

If you want to be able to import from website, then you need '/home/Mp08100/mysite' and not '/home/Mp08100/mysite/website' to be added to your sys path in your wsgi file.