Iam using PythonAnywhere free version to try set up a small rest service for an app. Iam also using flask with blueprints to attempt to do this. I have my file structure looks like below
mysite/
applicaction/
__init__.py,
models.py,
config.py
flask_app.py (same level as mysite package)
I use flask_app.py to run the server shown below.
from application import __init__ as appmain
if __name__ == "__main__":
myapp = appmain.getapp()
myapp.run()
and inside my application folder i use "init.py" to set up my "app" instance along with the blueprint called "myblueprint" (code shown below)
from flask import Flask, Blueprint
from flask.ext.sqlalchemy import SQLAlchemy
db = None
app = None
serviceblueprint = None
def init_app():
app = Flask(__name__)
app.config.from_object('config')
serviceblueprint = Blueprint('serviceblueprint', __name__)
app.register_blueprint(serviceblueprint)
db = SQLAlchemy(app)
def get_app():
init_app()
return app
then I try to define a simple route decorator using the registered blueprint in my service.py script having imported "serviceblueprint". Ideally id like serive.py module to contain a class that can handle routes (not sure if this is possible) but ill cross that bridge when I get to it. for now its a basic script (shown below)
from application import serviceblueprint
@serviceblueprint.route('/')
def get():
return "<h1>Yahoo!!!</h1>"
unfortunately I just get an error page when I load my main url into the browser and a relatively generic tracelog that i dont understand on the server. It doesnt seem to point to anything in my code at least. except maybe "ImportError: cannot import name app" but I am not importing "app" in any of my scripts a present.
2015-10-13 09:46:59,068 :Traceback (most recent call last):
2015-10-13 09:46:59,068 : File "/bin/user_wsgi_wrapper.py", line 134, in __call__
2015-10-13 09:46:59,068 : self.error_log_file.logger.exception("Error running WSGI application")
2015-10-13 09:46:59,068 : File "/usr/lib/python2.7/logging/__init__.py", line 1185, in exception
2015-10-13 09:46:59,069 : self.error(msg, *args, **kwargs)
2015-10-13 09:46:59,069 : File "/usr/lib/python2.7/logging/__init__.py", line 1178, in error
2015-10-13 09:46:59,069 : self._log(ERROR, msg, args, **kwargs)
2015-10-13 09:46:59,069 : File "/usr/lib/python2.7/logging/__init__.py", line 1270, in _log
2015-10-13 09:46:59,069 : record = self.makeRecord(self.name, level, fn, lno, msg, args, exc_info, func, extra)
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/logging/__init__.py", line 1244, in makeRecord
2015-10-13 09:46:59,070 : rv = LogRecord(name, level, fn, lno, msg, args, exc_info, func)
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/logging/__init__.py", line 284, in __init__
2015-10-13 09:46:59,070 : self.threadName = threading.current_thread().name
2015-10-13 09:46:59,070 : File "/usr/lib/python2.7/threading.py", line 1160, in currentThread
2015-10-13 09:46:59,070 : return _active[_get_ident()]
2015-10-13 09:46:59,070 : File "/bin/user_wsgi_wrapper.py", line 126, in __call__
2015-10-13 09:46:59,071 : app_iterator = self.app(environ, start_response)
2015-10-13 09:46:59,071 : File "/bin/user_wsgi_wrapper.py", line 140, in import_error_application
2015-10-13 09:46:59,071 : raise e
2015-10-13 09:46:59,071 :ImportError: cannot import name app
If anyone can help me out id be grateful. I am brand new to flask aswell so obviously I could be making a very simple error. thanks!
If you check your webapps tab where all your currently hosted websites are, you will see a wsgi.py file. Inside of that, you will see that it tries to import app
from your project files.
Make sure that it is able to do this successfully.