Forums

Error running Flask App.

I changed the structure of my flask app to utilize modules. The app works locally, but not sure why its running into a problem here. Any ideas?

pl2flask.py

#coding: utf-8
__author__ = 'bruno'
import os
if os.path.exists('.env'):
    print('Importing environment from .env...')
    for line in open('.env'):
        var = line.strip().split('=')
        if len(var) == 2:
            os.environ[var[0]] = var[1]
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from flask import send_from_directory, abort
from scripts import database_setup

import os


from settings import create_app, db
app = None

try:
    app = create_app(os.getenv('FLASK_CONFIG'))
    app.config.from_object(os.environ['APP_SETTINGS'])
except Exception, ex:
    app = create_app('default')
    print('FLASK_CONFIG or APP_SETTING not found. Using default settings(development)')

@app.errorhandler(404)
def page_not_found(e):
    return 'Page not found!!!', 404

@app.route('/uploads/<tipo_registro>/<id_registro>/<filename>')
def uploaded_file(tipo_registro, id_registro, filename):
    if '/' in filename or '\\' in filename:
       abort(404)
    path = '%s/%s/' % (tipo_registro, id_registro)
    final_path = os.path.join(app.config['UPLOAD_FOLDER'], path)
    return send_from_directory(final_path, filename)
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)

if __name__ == '__main__':
    database_setup.inserir_cliente_dgr()
    database_setup.inserir_usuario_dgr()
    database_setup.inserir_permissoes_bd()
    database_setup.inserir_grupos_usuarios()
    database_setup.dar_permissoes_ao_grupo()
    database_setup.inserir_usuario_grupo()
    database_setup.inserir_local_raiz()

    manager.run()

WSGY.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/brunocodeman/pl2flask'
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 pl2flask import app as application

Error Log:

2015-06-18 07:49:26,619 :/usr/lib/python2.7/threading.py:1160: RuntimeWarning: tp_compare didn't return -1 or -2 for exception
2015-06-18 07:49:26,620 :  return _active[_get_ident()]
2015-06-18 07:49:26,618 :Error running WSGI application
None
2015-06-18 07:49:32,602 :Error running WSGI application
None
2015-06-18 07:49:33,086 :Error running WSGI application
None

WSGI applications are not run as main modules, so none of the code in the if __name__ == "__main__": block is run. Also, the manager.run() is not necessary and it looks to me like the try..except around create_app is failing because the environment variables are not set. You'll need to set the environment variables that your app needs in your wsgi.py using os.environ.

Hello Glenn,

Can you be a little more specific? I have pretty much the same problem bruno (404 error) has but I have no clue how the wsgi.py file should look like with os.environ. I do not have app.run() in my flask files as I read it is not necessary.

My application structure

/khaleesicode
    /mysite
        __init__.py
        site_views.py
        api.py
        /configs
        /modules
        /static
        /templates

init.py

from flask import Flask
app = Flask(__name__)
app.config.from_pyfile('/home/ciacicode/khaleesicode/mysite/configs/khal_config.cfg')

import mysite.site_views
import mysite.api

www_khaleesicode_com_wsgi.py

import os
import sys

path = '/home/ciacicode/khaleesicode'
if path not in sys.path:
    sys.path.append(path)

from mysite import app as application

All my @app.route are ignored and only my error handler in api.py loads (visit www.khaleesicode.com) to see.

Thanks for your help, M.

Solved!

My set up was correct, but I had left a SERVER_NAME in my config that was messing up everything else. This is the post that helped me on stack overflow! http://stackoverflow.com/questions/24437248/unexplainable-flask-404-errors