Harrison Harrison - 4 months ago 15
Python Question

Flask: NameError: 'app' is not defined?

My Flask application has been running fine for the last 24 hours, however I just took it offline to work on it and i'm trying to start it up again and i'm getting this error:

Traceback (most recent call last):
File "runserver.py", line 1, in <module>
from app import app
File "/home/MY NAME/APP FOLDER NAME/app.py", line 15, in <module>
from Views import *
File "/home/MY NAME/APP FOLDER NAME/Views.py", line 1, in <module>
@app.route('/contact', methods=('GET', 'POST'))
NameError: name 'app' is not defined

I am running the application currently by calling
python runserver.py


from app import app

app.run(threaded = True, debug=True, host='')

Views.py: contains all of my routes, I won't post them all, as the error is coming from the first time
is mentioned in this file.

@app.route('/contact', methods=('GET', 'POST'))
def contact():
form = ContactForm()

if request.method == 'POST':
msg = Message("CENSORED,
msg.body = """
From: %s <%s>,
""" % (form.name.data, form.email.data, form.message.data)
return "><p><br>Successfully sent message!</p></body>"
elif request.method == 'GET':
return render_template('contact.html', form=form)

app.py: Here is the top of my app.py file, where I define
app = Flask(__name__)
as well as import my statements.

from flask import Flask, request, render_template, redirect, url_for, send_file
from geopy.geocoders import Bing
from geopy.exc import GeocoderTimedOut
import re
import urllib
from bs4 import BeautifulSoup
from openpyxl import load_workbook
from openpyxl.styles import Style, Font
import os
import pandas as pd
import numpy as np
import datetime
from Helper_File import *
from Lists import *
from Views import *
from flask_mail import Mail, Message
from forms import ContactForm

global today

geolocator = Bing('Censored')

app = Flask(__name__)

EDIT: I have made the changes suggested in the answer below, but am getting this when I access the page:

Not Found

The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.

Here is my file structure:

|- seperate html file for each page template


In app.py you should remove the from Views import *. Instead in your Views.py you need to have from app import app

That will bring app into the views namespace and I believe that should work for you.