Forums

Flask - Error running WSGI application - ImportError: cannot import name 'app'

Python and Flask beginner and completely new to Python Anywhere so please be really basic in your responses. I followed a tutorial to get Flask up and running on my PythonAnywhere account but then realized it was going to be really cumbersome to build out the application on PA so I built it locally. I've uploaded all of the changes to PA, fixed the first issue/ error message but now I'm stumped on the second. I've gone through all of the search results for similar issues and none of the solutions are panning out for me. I'm guessing it's a simple issue with my configuration in WSGI or the Python script interface. Please help and thanks in advance. - Scott

Error message is as follows:

2018-10-31 23:27:45,927: Error running WSGI application
2018-10-31 23:27:45,932: ImportError: cannot import name 'app'
2018-10-31 23:27:45,932:   File "/var/www/larsen_pythonanywhere_com_wsgi.py", line 111, in <module>
2018-10-31 23:27:45,933:     from WW_RSVP import app as application  # noqa

Relevant part of WSGI file:

import sys
path = '/home/larsen/WW_RSVP'
if path not in sys.path:
    sys.path.insert(0, path)
    # sys.path.append(path)

from WW_RSVP import app as application  # noqa

Can't seem to get this to format properly but everything after this is the WW_RSVP.py (main flask file) within the /home/larsen/WW_RSVP directory.


import gspread, datetime from oauth2client.service_account import ServiceAccountCredentials from flask import Flask, render_template, request, make_response

from flask import Flask, flash, redirect, render_template, request, session, make_response

from flask_session import Session

from werkzeug.exceptions import default_exceptions

from tempfile import mkdtemp

app = Flask(name)

Ensure templates are auto-reloaded

app.config["TEMPLATES_AUTO_RELOAD"] = True

scope = ['https://spreadsheets.google.com/feeds']

scope = ['https://spreadsheets.google.com/feeds', 'https://www.googleapis.com/auth/drive'] creds = ServiceAccountCredentials.from_json_keyfile_name('client_secret.json', scope) client = gspread.authorize(creds)

# Configure session to use filesystem (instead of signed cookies)

app.config["SESSION_FILE_DIR"] = mkdtemp()

app.config["SESSION_PERMANENT"] = False

app.config["SESSION_TYPE"] = "filesystem"

Session(app)

spreadsheet = client.open('WaynewoodReservations') def worksheet(sheet): return spreadsheet.worksheet(sheet)

lots = ['2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '14', '16', '17', '18', '19', '20', '21', '22', '23', '24', '25', '26', '27', '28', '29', '30', '31', '32', '33', '34', '35', '36', '37', '38', '39', '40', '41', '42', '43', '44', '45', '46', '47', '48', '49', '51', '52', '53', '54', '55', '56', '57', '58', '59', '60', 'C']

rows_to_lots = {2: 'C', 3: 2, 4: 3, 5: 4, 6: 5, 7: 6, 8: 7, 9: 8, 10: 9, 11: 10, 12: 11, 13: 12, 14: 14, 15: 16, 16: 17, 17: 18, 18: 19, 19: 20, 20: 21, 21: 22, 22: 23, 23: 24, 24: 25, 25: 26, 26: 27, 27: 28, 28: 29, 29: 30, 30: 31, 31: 32, 32: 33, 33: 34, 34: 35, 35: 36, 36: 37, 37: 38, 38: 39, 39: 40, 40: 41, 41: 42, 42: 43, 43: 44, 44: 45, 45: 46, 46: 47, 47: 48, 48: 49, 49: 51, 50: 52, 51: 53, 52: 54, 53: 55, 54: 56, 55: 57, 56: 58, 57: 59, 58: 60} lots_to_rows = {'C': 2, 2: 3, 3: 4, 4: 5, 5: 6, 6: 7, 7: 8, 8: 9, 9: 10, 10: 11, 11: 12, 12: 13, 14: 14, 16: 15, 17: 16, 18: 17, 19: 18, 20: 19, 21: 20, 22: 21, 23: 22, 24: 23, 25: 24, 26: 25, 27: 26, 28: 27, 29: 28, 30: 29, 31: 30, 32: 31, 33: 32, 34: 33, 35: 34, 36: 35, 37: 36, 38: 37, 39: 38, 40: 39, 41: 40, 42: 41, 43: 42, 44: 43, 45: 44, 46: 45, 47: 46, 48: 47, 49: 48, 51: 49, 52: 50, 53: 51, 54: 52, 55: 53, 56: 54, 57: 55, 58: 56, 59: 57, 60: 58}

def get_worksheet_titles(spreadsheet): worksheet_list = spreadsheet.worksheets() worksheet_titles = [] for worksheet in worksheet_list: title = str(worksheet).split('\'') worksheet_titles.append(title[1]) return worksheet_titles

def make_reservation(event, lot, number): l = lots_to_rows[lot] worksheet(event).update_cell(l,4, number)

@app.route("/", methods=["GET", "POST"]) def index():

if 'lot' not in locals():
    if 'lot' in request.cookies:
        lot = request.cookies.get('lot')
        if lot == 'C':
            pass
        else:
            lot = int(lot)

if request.method == "POST":
    print('Triggered top if POST')

    if not request.form.get("event"):
        return apology("Please choose an event.", 400)

    try:
        number = request.form.get("number")
    except:
        pass
    try:
        number = request.form.get("num")
    except:
        pass
    if 'number' not in locals():
    # elif not request.form.get("number") or not request.form.get("num"):
        return apology("How many people are coming?", 400)

    elif not request.form.get("lot"):
        if not 'lot' in locals():
            return apology("Which lot are you RSVP'ing for?", 400)

    event = request.form.get("event")
    try:
        number = int(request.form.get("num"))
    except:
        number = int(request.form.get("number"))

    if 'lot' not in locals():
        lot = int(request.form.get("lot"))

    try:
        make_reservation(event, lot, number)
        reservation = True
            # f'Reservation is set to {reservation}'

    except:
        print('Entry didn\'t work.')

    if reservation == True:
        resp = make_response(render_template("index.html", worksheet_titles=get_worksheet_titles(spreadsheet), lot=lot, event=event, number=number))
        if 'lot' not in request.cookies:
            expire_date = datetime.datetime.now()
            expire_date = expire_date + datetime.timedelta(days=9000)
            resp.set_cookie('lot', str(lot), expires=expire_date)
        return resp

else:
    print('Triggered bottom except')
    return render_template("index.html", worksheet_titles=get_worksheet_titles(spreadsheet), lots = lots)

if name == 'main': app.run()

That just means there is no 'app' in the first WW_RSVP on your python path. See here: http://help.pythonanywhere.com/pages/DebuggingImportError/ for how to understand what's going on and how to debug it.