I have a Flask app that runs fine on local, but when I push to Heroku I get the error message:
* Running on http://127.0.0.1:5000/
[INFO] Starting gunicorn 18.0
[ERROR] Connection in use: ('0.0.0.0', 8163)
I tried the solution in this question, where gunicorn and werkzeug were fighting with each other, but adding a __name__ == "__main__" block to my master application file (run.py) didn't solve my error.
This SO post suggested making sure my processes were all cleaned up. I did that but it still didn't work, so I actually deleted my entire heroku app and repushed and it still gives the same error.
My run.py file looks like:
#!pl_env/bin/python
from app import app
if __name__ == "__main__":
app.run(debug=True, port=33507)
# [33507 is the Flask port on Heroku]
And my Procfile is:
web: gunicorn run:app
The __init__.py file in app is:
from flask import Flask
import os
from flask.ext.login import LoginManager
app = Flask(__name__)
app.config.from_object('config')
lm = LoginManager()
lm.init_app(app)
lm.login_view = 'login'
from app import views
Views has the meaty logic of the app in it, but I don't think there's anything in there that would mess with gunicorn. Here's the views.py imports just in case:
from flask import Flask, render_template, flash, redirect
from flask.ext.login import login_required, login_user
from app import app, lm
from forms import LoginForm
I am definitely at a loss as to where to look now to figure out this connection error. Any thoughts on why it seems to think I'm already using 0.0.0.0?
Thank you
Monica
EDIT: I ran the app locally using foreman start and it worked cleanly in 0.0.0.0:5000, so I think I'm having a Heroku problem
EDIT2: I intentionally broke my view flow -- made an internal infinite reference -- and pushed it to see what would happen. I got the expected error logs, undid it, and pushed the rollback, and now it's working. I have absolutely no idea why that should work, except maybe that breaking it in an expected way flushed my gunicorn connections. If anyone has any explanations for this mystery, I would love them.
app.run(host='0.0.0.0')
might do the trick as explained here
Related
If I understand the documentation correctly, in Python3 Flask ___init___.py is used for importing classes and can be completely empty.
I am trying to use app.py to hold my code, but I don't know how to get Flask to execute app.py. Code I put in __init__.py will execute, but I don't think I should do it that way. I have seen some posts where doing this could cause code to execute when it was just importing.
How do I get Flask to hit my app.py instead of __init__.py?
Here is my wsgi config file
import sys
import logging
logging.basicConfig(stream=sys.stderr)
sys.path.insert(0,"/var/www/FlaskApp/")
from FlaskApp import app as application
application.secret_key = 'something secret'
In my wsgi.py I just import the app (from app.py) and run it. I do have an older setup though but the approach should still apply.
from app import app
if __name__ == "__main__":
app.run()
and my app.py has the global Flask app variable along with all the other setup and Flask code (logging, routes, etc.)
app = Flask(__name__)
Did you set the FLASK_APP environment variable to your app.py file?
$ export FLASK_APP=app
$ flask run
See: https://flask.palletsprojects.com/en/1.1.x/cli/#application-discovery
I'm a total beginner with heroku and API's but I needed to execute some python for a mobile app project.
Anyway I watched a ton of tutorial and looked for the doc on heroku website but my problem remains the same :
I push my project using $ git push heroku master but when loading the page where my API should appear I get an application error from heroku which tells to check the logs.
So in the logs I am facing the following error : << ModuleNotFoundError: No module named 'app' >>
I have this code :
app.py
import flask
app = flask.Flask(__name__)
#app.route('/')
def home():
return 'hello'
Procfile
web: gunicorn testdamien.wsgi
wsgi.py
from app import app
if __name__ == '__main__':
app.run()
I don't really know what's the use of the wsgi file, I saw some youtubers not using it and directly putting the app.run() in the app.py.
Also, I tried with the following Procfile :
web: gunicorn app:app
don't work either...
Thank you for reading me.
So in your app.py your application is not running. Edit the file as below:
import flask
app = flask.Flask(__name__)
#app.route('/')
def home():
return 'hello'
if __name__ == '__main__':
app.run(host='0.0.0.0')
in your Procfile you are specifying your entry-point the format is web: gunicorn your_module_name:your_application name In your case it will be:
web: gunicorn app:app
Should work fine
When I try to update the __init__.py file in Flask, it doesn't show the changes in the server, but when I edit home.html it works fine.
app/__init__.py
import os
from flask import Flask, render_template
from werkzeug.contrib.fixers import ProxyFix
app = Flask(__name__)
#app.route('/')
def home():
return render_template('home.html')
app.wsgi_app = ProxyFix(app.wsgi_app)
app.debug = bool(os.environ.get('PRODUCTION'))
if __name__ == '__main__':
app.run()
Any tips?
We solved the problem in comments but I will add solution here if someone else has a similar problem.
For development environment add debug=True argument to your app
app.run(debug=True)
If your development environment works on an application server, then you should look for autoreload option. In uWSGI there is py-auto-reload for example.
For released, stable environment you should restart your application server.
For example in uWSGI
There are several ways to make uWSGI gracefully restart.
# using kill to send the signal
kill -HUP `cat /tmp/project-master.pid`
# or the convenience option --reload
uwsgi --reload /tmp/project-master.pid
# or if uwsgi was started with touch-reload=/tmp/somefile
touch /tmp/somefile
More: http://uwsgi-docs.readthedocs.io/en/latest/Management.html#reloading-the-server
Warning: if you combine application and web server, uWSGI and Nginx for example, then restarting Nginx won't reload your application code. Focus on the application server.
based on this structure: http://flask.pocoo.org/docs/patterns/packages/
I also tried this post: Deploying Flask app to Heroku
I am having trouble getting this to work on heroku. I usually get the PORT does not set within 60 seconds error. I have read other SO posts and just can't figure out if my project structure is wrong or my procfile. I tried other ports than 5000 as well.
Here is my current project structure:
/myapplication
Procfile
runserver.py
/applicationfolder
__init__.py
views.py
Here is my Procfile
web: python runserver.py $PORT
Here is my runserver.py
from applicationfolder import app
app.run()
if __name__ == '__main__':
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
Here is my init.py
import os
from flask import Flask
from flask import render_template, jsonify, request
app = Flask(__name__)
app.config.from_object('config')
import applicationfolder.views
From there views.py runs.
This works locally with foreman start and python runserver.py, but does not work with heroku. I have tried many things with PORT but port doesn't seem to set even with a different PORT than 5000. I think it has something to do with my project structure.
The app.run() was in there twice, which as you noted is what's screwing things up. The app.run() invokes a simply pure-python development server so that you can easily run and/or debug your script.
By invoking it at the module level (right under your import in runserver.py), you were effectively trying to start the development server as the python code was loaded, and then when it went to run it when invoked from the Procfile, the development server was already in flight, having been starting with it's defaults (latest version of Flask is pulling relevant defaults from the SERVER_NAME environment variable). By having it in both places, you were trying to invoke that method twice.
You basically want either the straight up module load (in which case, kill off the code under "if name ...", or you use the code when invoking under main, in which case don't start the service at module load time.
Twilio newbie question:
I created an app that uses the Twilio API as I followed along to the tutorial by General Assembly
The files I added are
app.py
Procfile
requirements.txt
app.py
from flask import Flask
from flask import request
from twilio import twiml
import os
app = Flask(__name__)
#app.route('/caller', methods=['POST'])
def caller():
response = twiml.Response()
response.enqueue("Christmas Queue")
return str(response)
if __name__ == "__main__":
port = int(os.environ.get('PORT', 5000))
app.debug = True
app.run=(host='0.0.0.0'. port=port)
Procfile
web: python app.py
requirements.txt
flask>=0.9
twilio>=3.1
I deployed the app to Heroku. And then I added the URL to Twilio
I called to test it, but got an error. Not sure what my next steps can be to troubleshoot this further.
Your first task should be to run heroku logs -t and actually look at the Heroku output when your app is deployed.
You have a few syntax errors:
app.run=(host='0.0.0.0'. port=port)
^ ^
Remove that equals sign, replace the period with a comma and your script will run.
I would read through Heroku's Python tutorial as well.