Blog, Pyresearch

Deploying Python Applications to Heroku

Deploying Python Applications to Heroku: A Step-by-Step Guide

Deploying your Python application to the cloud is an essential skill for any developer who wants to make their project available online. Heroku, a platform-as-a-service (PaaS), is a popular choice for hosting Python applications because of its simplicity and integration with Git.

In this guide, we’ll walk through the process of deploying a Python web app to Heroku, covering all the steps from setting up the app to making it live online.

_______________________________________________________________________

Prerequisites

Before getting started, make sure you have the following:

  1. Python installed on your computer.
  2. Git installed (needed to push your code to Heroku).
  3. A Heroku account (you can create one here).

_______________________________________________________________________

Step 1: Set Up Your Python Application

First, you need a Python application to deploy. For this guide, we’ll create a simple Flask web app as an example. If you don’t already have one, create the following files in your project directory.

app.py (The Main Application File)

from flask import Flask

app = Flask(__name__)

@app.route('/')
def home():
    return "Hello, Heroku!"

if __name__ == '__main__':
    app.run(debug=True)

Install Flask

Before running the app, install Flask using the following command:

pip install flask

_______________________________________________________________________

Step 2: Prepare Your Application for Heroku

Heroku expects some specific files in your project to understand how to run the application. Let’s set up those files.

1. requirements.txt (List of Dependencies)

Heroku needs to know which libraries your app requires. You can generate a requirements.txt file by running the following command:

pip freeze > requirements.txt

This command lists all the installed Python packages and their versions in requirements.txt.

2. Procfile (Specifying the Application Type and Command)

Create a file called Procfile (without an extension) in your project directory. This file tells Heroku how to run your app.

Procfile content:

web: gunicorn app:app

Here, we are specifying that the app should be run using Gunicorn, a popular web server for running Python apps.

Install Gunicorn

You also need to add Gunicorn to your project by installing it and adding it to the requirements.txt.

pip install gunicorn
pip freeze > requirements.txt

_______________________________________________________________________

Step 3: Initialize Git in Your Project

Heroku uses Git to deploy applications. If you haven’t initialized Git in your project yet, do so now:

git init

Next, add your files to the repository and commit the changes:

git add .
git commit -m "Initial commit"

_______________________________________________________________________

Step 4: Deploy to Heroku

1. Install the Heroku CLI

To interact with Heroku from the command line, you need to install the Heroku CLI. You can download and install it from here.

2. Log in to Heroku

Once the Heroku CLI is installed, log in to your Heroku account by running:

heroku login

This will open a web browser where you can log in to your Heroku account.

3. Create a New Heroku App

To create a new app on Heroku, use the following command:

heroku create your-app-name

This will create a new Heroku app and give you a URL where your app will be hosted. If you don’t specify an app name, Heroku will generate a random name for you.

4. Deploy Your Application

Now that your app is set up on Heroku, you can deploy it by pushing your code to Heroku’s Git repository:

git push heroku master

After the deployment is complete, you’ll see a message like this:

remote: -----> Launching...
remote:        Released v1
remote:        https://your-app-name.herokuapp.com/ deployed to Heroku
remote: Verifying deploy... done.

You can now open your deployed application in a browser by running:

heroku open

_______________________________________________________________________

Step 5: Manage Your Application

1. Viewing Logs

If you encounter any issues with your app, you can view the logs to troubleshoot:

heroku logs --tail

This command will display the real-time logs of your Heroku app.

2. Scaling the Application

By default, your app runs with one web dyno (a lightweight container used to run your app). If you need to scale your app (e.g., for higher traffic), you can add more dynos:

heroku ps:scale web=1

_______________________________________________________________________

Conclusion

Deploying a Python application to the cloud with Heroku is simple and quick, making it an ideal choice for beginners and small projects. You’ve learned how to set up your Python app, prepare it for deployment, and push it to Heroku using Git. Now your Python application is live on the internet, ready for the world to see!

author-avatar

About Muhammad Adeel Ashraf

Muhammad Adeel Ashraf is a Co-founder of Pyresearch, Adeel Ashraf is a pioneer in AI innovation who is committed to creating game-changing AI solutions. Pyresearch is a cutting-edge AI startup that provides businesses with cutting-edge machine learning, Deep Learning, and Computer Vision technology. Pyresearch focuses on providing state-of-the-art AI-driven research, consultancy, and customized solutions. With the mission of using artificial intelligence to spur innovation and growth, Pyresearch is dedicated to assisting businesses in realizing their potential in the AI-driven future.

Leave a Reply

Your email address will not be published. Required fields are marked *