Forums

Error running WSGI application

Hello everyone, I have just updated my Flask code and it suddenly stopped working. When I try to go the index page I get "Error code: Unhandled Exception". Error log:

2017-01-29 14:39:05,912 :Error running WSGI application
2017-01-29 14:39:05,964 :ImportError: cannot import name 'app' 
2017-01-29 14:39:05,965 : File "/var/www/neonbevz_pythonanywhere_com_wsgi.py", line 16, in <module> 
2017-01-29 14:39:05,965 :    from test import app as application

Code for index:

from flask import Flask, render_template, request, session
from generator import *
app = Flask(__name__)
app.secret_key = '01010101011100001010'

@app.route("/")
def index():
    session["A"] = random.randint(10,40)
    session["Y"], session["P"] = num_gen()
    session["a"] = halfkey_gen(session["Y"], session["A"], session["P"])
    return render_template('index.html', Y=session["Y"], P=session["P"], a=session["a"])

I didn't change anything except index(). Any ideas how to solve this? Thanks in advance.

Hm, it's definitely strange that just changing the index would cause an import error. But you should try and get to the bottom of that first: http://help.pythonanywhere.com/pages/DebuggingImportError

@harry I have located the config file and tried to find the "test" module or folder, but it seems that it's not in this directory. is this OK?

18:31 /var/www $ cat neonbevz_pythonanywhere_com_wsgi.py
# This file contains the WSGI configuration required to serve up your
# web application at http://<your-username>.pythonanywhere.com/
# It works by setting the variable 'application' to a WSGI handler of some
# description.
#
# The below has been auto-generated for your Flask project
import sys
# add your project directory to the sys.path
project_home = u'/home/neonbevz/project'
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 test import app as application
18:32 /var/www $ dir
__pycache__  neonbevz_pythonanywhere_com_wsgi.py  neonbevz_pythonanywhere_com_wsgi.py.2016-12-26-23-33.bak
18:32 /var/www $ cd test
bash: cd: test: No such file or directory

Hi there, I'm guessing that the "test" in "from test import app as application" is probably meant to be in the /home/neonbevz/project folder? That's the place where you import your main flask app. Is you flask app in a file called "test.py"? Maybe the "from test" is wrong?

i am having a the same error for a very simple hello world flask application

Is this an error importing your Flask application (eg. a ModuleNotFoundError)? If so, check out this help page.