my heroku app runs into application error with the following logs:
Starting process with command `gunicorn run:app`
Failed to find application: run
here is my Procfile
web: gunicorn run:flask_app
run.py file
from app import create_app, db
from app.auth.models import User
if __name__ == '__main__':
flask_app = create_app('prod')
with flask_app.app_context():
db.create_all()
if not User.query.filter_by(user_name='harry').first():
User.create_user(user='harry', email='harry#potters.com', password='secret')
flask_app.run()
Your main-method does not get executed (AFAIK) because Gunicorn is calling your script, you are not executing it as the main-file. Try moving the flask_app out of your main-method to the top of the file so Gunicorn can actually find it when importing your run.py!
Related
Trying to follow best practices for a Flask app running in Heroku so I'm moving things from app.py to working with blueprints.
The current directory structure is as follows:
--root
--application
--admin_blueprint
--another_blueprint
--wsgi.py (app = create_app())
--__init__.py (this has def create_app, which handles creating my app)
--migration
--Procfile
--requirements.txt
--runtime.txt
--config.py
--manage.py
This is init.py
from flask import Flask
...
def create_app():
app = Flask(...)
...
return app
and this is wsgi.py
from application import create_app
app = create_app()
if __name__ == '__main__':
app.run(host='0.0.0.0')
I cannot for the life of me figure out how to do the Procfile correctly, this is what I previously had when I had app.py and wsgi.py in my root directory and it was working fine on Heroku:
web: gunicorn app:wsgi
I've tried some of these:
web: gunicorn application:wsgi
web: gunicorn application.wsgi
web: gunicorn --pythonpath application application:wsgi
web: gunicorn application.wsgi.py
web: gunicorn "application.wsgi.py"
web: gunicorn "application/wsgi.py"
flask run works because I've exported FLASK_APP=application.wsgi.py
Thank you.
Use application.wsgi:app
application.wsgi (the part before the colon) instructs gunicorn how to resolve the module.
and app (the part after the colon) gives the name of the WSGI application declared in the resolved module.
:app can be omitted and gunicorn defaults to looking in the module for a WSGI application with the name application.
This question already has answers here:
Flask and uWSGI - unable to load app 0 (mountpoint='') (callable not found or import error)
(3 answers)
Closed 6 years ago.
I am trying to run my Flask app under uWSGI and am getting:
unable to load app 0 (mountpoint='') (callable not found or import error)
*** no app loaded. going in full dynamic mode ***
The layout is:
/opt/myapp
/opt/myapp/wsgi.py
/opt/myapp/run.py
/opt/myapp/lib
/opt/myapp/app
/opt/myapp/app/blueprints.py
/opt/myapp/app/filters
/opt/myapp/app/filters/__init__.py
/opt/myapp/app/__init__.py
/opt/myapp/app/main.py
app/__init__.py contains the usual:
from flask import Flask
app = Flask(__name__)
app/main.py looks like:
import blueprints
import filters
from app import app
def run(debug, host='0.0.0.0'):
app.run(debug=debug, host=host)
wsgi.py looks like:
if __name__ == '__main__':
from app.main import app as application
application.run(host='0.0.0.0')
If I run python wsgi.py from the CLI, it works fine, the usual :5000 server.
If I run:
uwsgi --socket 0.0.0.0:8080 --protocol=http -w wsgi
I see the error, it cannot load the application.
uWSGI imports your wsgi.py. So this code is never executed:
if __name__ == '__main__':
from app.main import app as application
application.run(host='0.0.0.0')
You should create the application at module level:
from app.main import app as application
if __name__ == "__main__":
application.run(...)
You must of course leave the .run() method inside the main block, because you don't want uWSGI to execute that.
Have you tried something like ...
uwsgi -s 0.0.0.0:8080 --protocol=http --module myapp --callable app
I'm not 100% sure the --module and --callable options are correct, because I don't have your actual code in front of me, might be something like --module myapp.app --callable main or some other variant
From the documentation on using uwsgi together.
http://flask.pocoo.org/docs/0.10/deploying/uwsgi/
If it's in virtualenv You have to activate it by adding:
execfile(activate_this, dict(__file__=activate_this))
And I think You should define project directory:
import sys
sys.path.append('/opt/myapp/app')
I have a Flask app I am trying to serve via Gunicorn.
I am using virtualenv and python3. If I activate my venv cd to my app base dir then run:
gunicorn mysite:app
I get:
Starting gunicorn
Listening at http://127.0.0.1:8000
DEBUG:mysite.settings:>>Config()
...
Failed to find application: 'mysite'
Worker exiting
Shutting down: master
Reason: App failed to load
Looking in /etc/nginx/sites-available I only have the file 'default'. In sites-enabled I have no file.
In my nginx.conf file I have:
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
App structure:
mysite #this is where I cd to and run gunicorn mysite:app
--manage.py
--/mysite
----settings.py
----__init__.py
in manage.py for mysite I have following:
logger.debug("manage.py entry point")
app = create_app(app_name)
manager = Manager(app)
if __name__ == "__main__":
manager.run()
In __init__.py file:
def create_app(object_name):
app = Flask(__name__)
#more setup here
return app
In my settings.py in the app directory
class Config(object):
logger.debug(">>Config()") #this logs OK so gunicorn is at least starting in correct directory
From inside the virtualenv if I run
print(sys.path)
I find a path to python and site-packages for this virtualenv.
From what I have read to start gunicorn it's just a matter of installing it and running gunicorn mysite:app
Running gunicorn from the parent directory of mysite I get the same failed to find application: 'mysite', App failed to load error, but don't get the DEBUG...Config() logged (as we are clearly in the wrong directory to start in). Running gunicorn from mysite/mysite (clearly wrong) I get and Exception in worker process ereor, ImportError: No module named 'mysite'.
Any clues as to how I can get gunicorn running?
You're pointing gunicorn at mysite:app, which is equivalent to from mysite import app. However, there is no app object in the top (__init__.py) level import of mysite. Tell gunicorn to call the factory.
gunicorn "mysite:create_app()"
You can pass arguments to the call as well.
gunicorn "mysite:create_app('production')"
Internally, this is equivalent to:
from mysite import create_app
app = create_app('production')
Alternatively, you can use a separate file that does the setup. In your case, you already initialized an app in manage.py.
gunicorn manage:app
I recently changed my Heroku Python Flask app from the 'small application' format to the 'simple package' format based from flask documentation (De-coupling everything in app.py into separate subdirectories)
The application runs correctly using
> python runserver.py
However, executing
gunicorn runserver:app --log-file=-
outputs:
"Starting gunicorn .... connection in use error" (loops forever)
My runserver.py configuration is:
from re3 import app
app.run(debug=True)
__init__.py configuration:
import os
from flask import Flask
from flask import render_template
app = Flask(__name__)
import views
view.py configuration:
from re3 import app
#app.route('/')
def index():
return 'Hello World!'
What is changing in the two executions?
The problem is that you run your application anytime runserver is imported. You only want that to happen when it's executed directly.
from re3 import app
if __name__ == '__main__':
app.run(debug=True)
Edit:
The usage for gunicorn is
$ gunicorn [OPTIONS] APP_MODULE
When you run gunicorn, it imports APP_MODULE. In your case, you've specified runserver. So while you don't import it yourself, gunicorn does. And before gunicorn can run app, runserver runs it.
I successfully deployed the app and ran the url its shows application error.
Checked the Log, it states:
* Running on http://127.0.0.1:5000/
Web process failed to bind to $PORT within 60 seconds of launch
Procfile
web: python run.py ${PORT}
run.py
from app import app
app.run(debug=False)
I also tried with
from os import environ
from app import app
app.run(debug=False, port=environ.get("PORT", 5000), processes=2)
In both the case the error still persist
views.py
#app.route('/')
#app.route('/login', methods=["GET","POST"])
def login():
....
That's really not how you run a Flask application in production. You need an actual server, such as gunicorn, and you point that to your app object:
web: gunicorn app:app
This is all fully explained in the Heroku tutorial.