Forums

Flask - no module named app

Hello! I would like to add my simple website on server but flask_app.py and views.py gives me no module error. I don't know why because app object is created in init file. I'll be thankful for any advices.

My folder structure:

\mysite
    \app
        \static
        \templates
             index.html
        views.py
        __init__.py
    flask_app.py

init.py

from flask import Flask

app = Flask(__name__)
from app import views

views.py

from flask import render_template
from app import app

@app.route('/')
@app.route('/index')
def index():
return render_template('index.html',title='example')

flask_app.py

from flask import Flask
from app import app

wsgi.py

import sys

# add your project directory to the sys.path
project_home = u'/home/librarysystem/mysite'
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 flask_app import app as application

The problem is that you're using app to mean two different things here. You have a variable called app defined in your __init__.py, and there's a directory called app. So when you have the code

from app import views

...you're asking Python to import views from your variable called app, not from the directory.

I suggest you rename the app directory, and adjust your imports to suit the new directory name -- then app will only refer to the variable, which is what you want.

You're right. Now everything works. Thank you!

Awesome, thanks for confirming!