Wednesday, November 26, 2025

thumbnail

Deploying a Python Web Application to Heroku

 Step 1: Prerequisites


Make sure you have:


Python installed (3.x recommended)


Git installed


Heroku CLI installed


A Python web framework project (Flask or Django)


Step 2: Prepare Your Python App


For example, with Flask, your app.py might look like:


from flask import Flask


app = Flask(__name__)


@app.route("/")

def home():

    return "Hello, Heroku!"


if __name__ == "__main__":

    app.run(debug=True)


Step 3: Create Required Files for Heroku

1️⃣ requirements.txt


Contains all Python dependencies. Generate it using:


pip freeze > requirements.txt


2️⃣ Procfile


Tells Heroku how to run your app. Create a file named Procfile (no extension) with:


web: gunicorn app:app



Notes:


app:app → first app is your filename (app.py), second app is the Flask instance


Install gunicorn in your requirements: pip install gunicorn


3️⃣ runtime.txt (Optional)


Specify Python version:


python-3.11.8


Step 4: Initialize Git Repository

git init

git add .

git commit -m "Initial commit"


Step 5: Create Heroku App

heroku login      # Opens browser to login

heroku create my-flask-app



This will create a random app name if you don’t provide one. The app URL will be like https://my-flask-app.herokuapp.com/


Step 6: Deploy to Heroku

git push heroku main   # or master if your branch is master


Step 7: Open Your App

heroku open



This opens your app in the browser.


Step 8: Logs & Debugging

heroku logs --tail



Useful if your app fails to start.


✅ Tips & Best Practices


Use a virtual environment to manage dependencies.


Environment variables: Store secrets with heroku config:set SECRET_KEY=value.


Database: For Django or Flask with PostgreSQL, use dj-database-url or SQLAlchemy with DATABASE_URL from Heroku.


Scaling: heroku ps:scale web=1 to make sure one dyno is running.


Free Dyno Limitations: Apps sleep after 30 mins inactivity on free plan.

Learn Fullstack Python Training in Hyderabad

Read More

Setting Up GitHub Actions for CI/CD in Python Projects

How to Use Git for Version Control in Full Stack Projects

Introduction to Git and GitHub for Full Stack Python Developers

Version Control and Deployment

At Our Quality Thought Training Institute in Hyderabad

Get Directions

Subscribe by Email

Follow Updates Articles from This Blog via Email

No Comments

About

Search This Blog

Powered by Blogger.

Blog Archive