Deploy Python Flask to Heroku
Install the Heroku CLI
Download and install the Heroku CLI.
If you haven’t already, log in to your Heroku account and follow the prompts to create a new SSH public key.
$ heroku login
Add the following files to the application’s root folder:
Heroku apps include a Procfile that specifies the commands that are executed by the app on startup.
Add the following line:
web: python run.py
Note: Change the run.py to your startup file.
Pipfile is used to specify the preferred python version and all the dependencies of the application.
Add the following lines:
url = "https://pypi.org/simple"
verify_ssl = true
name = "pypi"[packages]
flask = "*"
adaboost-model = "0.1.0"[dev-packages][requires]
python_version = "3.7"
Note: Make sure to specify the correct python version and include all the required packages of you applicaiton.
Change the host of the flask application to this:
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
Generate the Pipfile.lock file using the following command:
$ pipenv lock
Note: Every time you have a changes in the Pipfile, you must run this command.
Create Heroku Application
- Login to Heroku site
- Go to Heroku dashboard
- Click New -> Create new app
- Enter the App name
- Click Create app button
- Go to Settings tab of you app
- Click the Add buildpack button
- Select buildpack (in this article, choose python)
- Click Save changes button
Create a new Git repository
Initialize a git repository in a new or existing directory
$ cd my-project/
$ git init
$ heroku git:remote -a app-name
Note: Change the app-name to your application name from Heroku.
Deploy your application
Commit your code to the repository and deploy it to Heroku using Git.
$ git add .
$ git commit -am "make it better"
$ git push heroku master
Heroku will generate a new link for your application’s public access.