Forums

Cannot import App module

Hello!

I modified heavily the app im working on, to avoid "Sql has goen away" exception, but the situation got worse. I update PYTHONPATH to the new path, and updated the working directory on website configuration page The present organization of the files is as follows:

/home/fabioquintilii
    /app
        runserver.py
            /app
                /static
                /templates
                __init.py__   <=== empty
                app.py
                config.py
                models.py
                views.py

When I run the app I get this error:

2016-08-28 21:56:34 Traceback (most recent call last):
2016-08-28 21:56:34   File "/bin/user_wsgi_wrapper.py", line 154, in __call__
2016-08-28 21:56:34     
2016-08-28 21:56:34 app_iterator = self.app(environ, start_response)
2016-08-28 21:56:34   File "/bin/user_wsgi_wrapper.py", line 170, in import_error_application
2016-08-28 21:56:34     
2016-08-28 21:56:34 raise e
2016-08-28 21:56:34 NameError
2016-08-28 21:56:34 : 
2016-08-28 21:56:34 name 'app' is not defined

app.py

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#!/usr/local/bin/python2.7
from flask  import Flask 
from flask_sqlalchemy import SQLAlchemy
from flask_sslify import SSLify
from config import *

import os

app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_POOL_SIZE'] = 100
app.config['SQLALCHEMY_POOL_RECYCLE'] = 280
app.config['DEBUG'] = True
app.secret_key = os.environ["SECRET_KEY"]
app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
db = SQLAlchemy(app)
sslify = SSLify(app)
import models, views

file wsgi

# 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/fabioquintilii/app/app'
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
from app import app as application

It seems that the server points to another directory, but I cannot understand how to fix it. Can You help me, please?

Regards

Fabio

Try removing the path-updating code from the WSGI file -- that might be interfering with your setup on the "Web" tab. That is, just delete (or comment out) these lines:

# add your project directory to the sys.path
project_home = u'/home/fabioquintilii/app/app'
if project_home not in sys.path:
    sys.path = [project_home] + sys.path

Sorry, didn't work I tried to put the app under application context but it raised " Working outside of application context".

I'm afraid that the reason of the issue could be that I created the app automatically, and when the application itself grew up I modified the directory "flask_app" to "app" and created new inner directory named "app", according to the official documentation. I have the necessity to "split" my app into two or three little apps, and make them work together.

I could erase this app and create another one manually, but I'd like to know if the database linked to my app is erased with, and if all the upgrades of Python library I downloaded locally are kept.

I would say don't have app inside app inside app. Just name them different things and your import error etc may be more clear (here we have no clue if you are one level too deep, or trying to import one level too shallow etc)

Great!!! It worked!!! Thank you very much! I simply renamed the root folder as "meda" and the inner folder as "comp".

May I ask You another question, please? I tried to use context application, so in app.py I wrote this:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
#!/usr/local/bin/python2.7
from flask  import Flask 
from flask_sqlalchemy import SQLAlchemy
from flask_sslify import SSLify
from config import *

import os

app = Flask(__name__)
with app.app_context():
  app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
  app.config['SQLALCHEMY_POOL_SIZE'] = 100
  app.config['SQLALCHEMY_POOL_RECYCLE'] = 280
  app.config['DEBUG'] = True
  app.secret_key = os.environ["SECRET_KEY"]
  app.config['SQLALCHEMY_DATABASE_URI'] = SQLALCHEMY_DATABASE_URI
  db = SQLAlchemy(app)
  sslify = SSLify(app)
import models, views

In views.py and models.py I simply recalled app object writing: app = current_app. The result was a runtime error "Working outside of application context" as I told before. Of course there is something I didn't understand reading the official documentation, anyway can you give me some idea about what could I missed?

If you're not doing app = current_app in a view or in the with block, then you're not in a piece of code where current_app makes sense, so Flask complains.

Here is the documentation for the Flask application context.