Skip to content

Instantly share code, notes, and snippets.

@bradtraversy
Created August 16, 2019 16:38
Show Gist options
  • Star 57 You must be signed in to star a gist
  • Fork 51 You must be signed in to fork a gist
  • Save bradtraversy/0029d655269c8a972df726ed0ac56b88 to your computer and use it in GitHub Desktop.
Save bradtraversy/0029d655269c8a972df726ed0ac56b88 to your computer and use it in GitHub Desktop.
Python & Postgres Heroku Deployment

Python Heroku Deployment

Steps to create a postgres database and deply a Python app to Heroku

Install guinicorn locally

pipenv install gunicorn
or
pip install gunicorn

Install Heroku CLI

https://devcenter.heroku.com/articles/heroku-cli

Login via CLI

heroku login

Create app

heroku create appname

Create database

heroku addons:create heroku-postgresql:hobby-dev --app appname

Get URI

heroku config --app appname

# Add to your app

Create Procfile

touch Procfile

# Add this
web: gunicorn app:app

Create requirements.txt

pip freeze > requirements.txt

Create runtime.txt

touch runtime.txt

# Add this
python-3.7.2

Deploy with Git

git init
git add . && git commit -m 'Deploy'
heroku git:remote -a appname
git push heroku master

Add table to remote database

heroku run python
>>> from app import db
>>> db.create_all()
>>>exit()

Visit app

heroku open
@guimatheus92
Copy link

I have a SQLITE db file already with data, and I'd like to deploy to Heroku. How can I do?
When I tried to create postgresql table on heroku python cli this error happened and won't let me create the table:

RuntimeError: No application found. Either work inside a view function or push an application context. See http://flask-sqlalchemy.pocoo.org/contexts/.

My app repository is in: https://github.com/guimatheus92/Game-Recommendation-System

My app file is in init.py:

# init.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager 
# For relative imports to work in Python 3.6
import os, sys; sys.path.append(os.path.dirname(os.path.realpath(__file__)))
import re

# init SQLAlchemy so we can use it later in our models
db = SQLAlchemy()

def create_app():
    app = Flask(__name__)

    app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False    
    app.config['SECRET_KEY'] = environ.get('SECRET_KEY')
    app.config['SQLALCHEMY_DATABASE_URI'] = environ.get('DATABASE_URL') or 'sqlite:///Games.db'
        
    db.init_app(app)

    login_manager = LoginManager()
    login_manager.login_view = 'auth.login'
    login_manager.init_app(app)

    from models import User

    @login_manager.user_loader    
    def load_user(user_id):
        # since the user_id is just the primary key of our user table, use it in the query for the user
        return User.query.get(int(user_id))

    # blueprint for auth routes in our app
    from auth import auth as auth_blueprint
    app.register_blueprint(auth_blueprint)

    # blueprint for non-auth parts of app
    from main import main as main_blueprint
    app.register_blueprint(main_blueprint)

    # blueprint for non-auth parts of app
    from games import games as main_blueprint
    app.register_blueprint(main_blueprint)

    # blueprint for non-auth parts of app
    from ml_utils import ml_utils as main_blueprint
    app.register_blueprint(main_blueprint)

    return app

@JigsAwe
Copy link

JigsAwe commented Apr 17, 2021

image
im at this part already, im having this problem, i appreciate any sort of help I can get.

@Rishavsinha19981
Copy link

image

I AM ABLE TO SEE THIS ERROR AFTER DEPLOYING IN HEROKU AND CREATING DATABASE ALSO BY CREATE_ALL() but then also this is showing up can ANY1 HELP ME PLZZZ

@esha123802
Copy link

image
im at this part already, im having this problem, i appreciate any sort of help I can get.

Im having the same error, did you figure it out? I have added the bin path to env also

@srinumadhavv
Copy link

Everything is running fine but unable to access the database , it says table not found

@lena-A-Al
Copy link

heroku open
is not working?

@Olney1
Copy link

Olney1 commented Sep 2, 2022

Hey Brad, could you add the CLI commands for viewing the database tables?

@Yemsquare
Copy link

Hey, having issue with creating the table with PostgreSQL

@kelderanyi
Copy link

heroku open is not working?

use this instead "heroku open --app appname

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment