I'm trying to use Heroku to push my Flask app to the web. I think I have everything down, except the Procfile. Here is what my file directory looks like:
My main.py is:
from application import app
My init.py is:
from flask import Flask
from config import Config
from flask_mongoengine import MongoEngine
app = Flask(__name__)
app.config.from_object(Config)
db = MongoEngine()
db.init_app(app)
from application import routes
What is the correct Procfile command here?
Thanks
Related
I'm trying to get a Flask "point of sale" application working on cPanel with no success.
Here is the directory structure on cPanel File Manager
I have my app = Flask(name) in init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
from flask_login import LoginManager
from flask_security import Security,SQLAlchemyUserDatastore
# from app.models import Role,User
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db, render_as_batch=True)
login = LoginManager(app)
from app import routes,models
I have tried using the line this line of code in my passenger_wsgi.py
from app import app as application
and in my pointofsale.py i used:
application = app
I have successfully created the python app and installed requirements.txt, when I try to load the link to the website but I cant reach the site.
Managed to figure out the issue, I had two files importing the application instance -: pointofsale.py and passenger_wsgi.py.
I deleted pointofsale.py, and added the following code to passenger_wsgi.py file
from app import app
application = app
I have a flask app deployed on Heroku right now. I wrote a script to update the database everyday and scheduled it to run everyday using Heroku Scheduler. The script runs fine but I am finding that it is not updating the database.
To deploy my app I have a wsgi.py
from my_project import create_app
app = create_app()
My Procfile
web: gunicorn wsgi:app
My script just imports a function I have in my init.py which looks like this:
def job(app):
with app.app_context():
# update database and execute job
My script:
from wsgi import app
from my_project import job
job(app)
My question is why the db is not updating. Is it because I have the app_context? So am I updating the db, but not in the correct context? Is it correct to import the app from wsgi.py? Is it the correct app that is being used in Heroku?
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 can run my flask app fine using flask's built-in web server command run(), but I'm receiving the following Import error while trying to run my app using gunicorn. I use the command gunicorn project.app:app from the mainfolder.
The error being thrown off from views.py is:
from app import db, url_for ImportError: cannot import name url_for
My app is organized as follows:
mainfolder/
Procfile
bin
project/
app.py
_config.py
views.py
run.py
run.py
from views import app
import os
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
app.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
import os
app = Flask(__name__)
app.config.from_pyfile('_config.py')
db = SQLAlchemy(app)
app.config['SECRET_KEY'], report_errors=False)
from views import *
if __name__ == '__main__':
port = int(os.environ.get('PORT', 5000))
app.run(host='0.0.0.0', port=port)
_config.py
import os
#All the app's keys
Procfile
#this works fine
web: python project/run.py
#this doesn't work
web: gunicorn project.app:app
python project/app.py runs the app fine so why does gunicorn project.app:app throw off a module ImportError?
Your import for url_for needs to come before app = Flask(__name__)
Your app is working on flask's server because url_for is imported prior to the
if __name__ == '__main__':
app.run(host='0.0.0.0', port=port)
statement. Gunicorn is getting the application before you have imported url_for.
My problem is similar to Flask and Gunicorn on Heroku import error and Procfile gunicorn custom module name but I can't seem to fix it using their solutions.
My Flask app has the following structure:
appname/
run.py
Procfile
venv/
...
appname/
app.py
views.py
run.py:
from appname import app
app.run(debug=True)
app.py:
from flask import Flask
app = Flask(__name__)
#app.route('/')
def home():
return "here"
Procfile:
web: gunicorn --pythonpath appname app:app
views.py:
from appname import app
from flask import render_template
#app.route('/there')
def there():
return "there"
Previously, I was experiencing erorrs when running foreman start, but those went away once I removed import appname.views from app.py.
Now, foreman start runs the app and route / is accessible but /there is not. How come?
Hooray! I've been able to have it work with the code I really wanted.
app structure (unchanged):
appname/
run.py
Procfile
venv/
...
appname/
app.py
views.py
run.py (unchanged):
from appname import app
app.run(debug=True)
app.py:
from flask import Flask
app = Flask(__name__)
import appname.views
import appname.anothermodule
Procfile:
web: gunicorn appname:app
views.py (unchanged):
from appname import app
#app.route('/')
def home():
return "Hello, awesomeness!"
I've been able to move "around" this issue, by:
Not having from appname import app anywhere else other than in run.py
Consequently, only defining routes in app.py
I would have preferred to keep my routes with my modules, although I am unsure which is the better Python style.