28
loading...
This website collects cookies to deliver better user experience
pip install gunicorn
pip install flask_sqlalchemy
pip install python-decouple
pip install psycopg2
web: gunicorn app:app
“app.py”
.web: gunicorn bot:app
pip freeze> requirements.txt
python-3.8.10
heroku login
heroku git:remote -a NAME_OF_YOUR_HEROKU_APP
heroku addons:create heroku-postgresql:hobby-dev
Now, log in to your Heroku dashboard, click on the Settings menu and then on the button Reveal Config Vars: by default you should have DATABASE_URI
configuration created after installing Postgres to Heroku. You can also add all the environment variables here.
.env
file or if it relies on variables found through Python’s os.environ, you must set them as well in order for your script to work. Typically a Telegram bot requires a token from Bot Father, this is where you add the token.heroku config:set [KEY_NANE=value …]
heroku config:set TOKEN=jfuurhj7367edjkjfkkjrfhre8nfdj
postgres
dialect name, the name postgresql
must be used instead now. The dialect is the part before the ://
in the URL. SQLAlchemy 1.3 and earlier showed a deprecation warning but still accepted it.postgres://
in the URL to postgresql://
.postgres
in the DATABASE_URL
they provide, which you probably use for SQLALCHEMY_DATABASE_URI
. To work around this until they update it, update the variable in the Heroku dashboard to use postgresql
.import os
import re
uri = os.getenv("DATABASE_URL") # or other relevant config var
if uri.startswith("postgres://"):
uri = uri.replace("postgres://", "postgresql://", 1)
# rest of connection code using the connection string `uri`
SQLALCHEMY_DATABASE_URI = uri
app.config['SQLALCHEMY_DATABASE_URI'] = uri
git add .
git commit -m "Deployment commit"
git push heroku master
heroku run python3
>>> from app import db
>>> db.create_all()
_heroku logs --tail_
ImportError: cannot import name 'db' from 'app' (unknown location)
Create a virtual env and install all packages. Ensure you have psycopg2
and gunicorn
Debug = True. You need to set Debug = False in your Flask Application when deploying to Heroku; Heroku usually does not allow you to run a production app in debug mode and it can cause your app to crash.
Flask Server Port
“5000”
that comes with Flask, It works with ports 443, 80, 88 or 8443.# Start Flask Server
if __name__ == "__main__":
app.run(host='0.0.0.0', port=int(os.environ.get('PORT', 5000)))