Deploying Python Web Apps on Heroku

Table of Contents

  1. Introduction
  2. Prerequisites
  3. Setup
  4. Creating a Heroku Account
  5. Installing the Heroku CLI
  6. Creating a Python Web App
  7. Setting Up a Git Repository
  8. Deploying the App on Heroku
  9. Conclusion

Introduction

In this tutorial, you will learn how to deploy a Python web application on Heroku. Heroku is a popular cloud platform that allows you to easily deploy, manage, and scale your web applications. By the end of this tutorial, you will have a fully functional Python web app running on Heroku.

Prerequisites

Before starting this tutorial, you should have the following:

  • Basic knowledge of Python programming
  • Familiarity with web development concepts (HTML, CSS, etc.)
  • A computer with Python installed

Setup

To get started, you need to set up a few things:

Creating a Heroku Account

  1. Open your web browser and go to the Heroku website.
  2. Click on the “Sign Up” button to create a new account.
  3. Fill in the required information and click on the “Create Account” button.
  4. Check your email for a verification link and follow the instructions to verify your account.

Installing the Heroku CLI

  1. Open a terminal or command prompt on your computer.
  2. Run the following command to install the Heroku CLI:

     $ curl https://cli-assets.heroku.com/install.sh | sh
    
  3. After the installation is complete, verify that the Heroku CLI has been installed by running the following command:

     $ heroku --version
    

    You should see the version number if the installation was successful.

Creating a Python Web App

  1. Create a new directory for your Python web app project.
  2. Open a terminal or command prompt and navigate to the project directory.
  3. Run the following command to create a virtual environment:

     $ python -m venv env
    
  4. Activate the virtual environment:

    • Windows:

        $ env\Scripts\activate
      
    • macOS/Linux:

        $ source env/bin/activate
      

      Setting Up a Git Repository

Next, you need to set up a Git repository to manage your project’s source code and track changes over time.

  1. Initialize a new Git repository in your project directory:

     $ git init
    
  2. Create a .gitignore file to specify which files and directories should be ignored by Git. Open a text editor and add the following lines:

     env/
     __pycache__/
     *.pyc
     *.pyo
     *.pyd
     .DS_Store
    
  3. Save the file as .gitignore in your project directory.

  4. Add all the files in your project directory to the Git repository:

     $ git add .
    
  5. Commit the changes:

     $ git commit -m "Initial commit"
    

    Deploying the App on Heroku

Now that your project is set up and your Git repository is ready, you can deploy your Python web app on Heroku.

  1. Login to Heroku from the command line:

     $ heroku login
    
  2. Enter your Heroku credentials when prompted.

  3. Create a new Heroku app:

     $ heroku create
    
  4. Verify that a new remote repository named “heroku” has been added to your Git configuration:

     $ git remote -v
    
  5. Deploy your app to Heroku:

     $ git push heroku master
    
  6. After the deployment is complete, run the following command to open your app in the browser:

     $ heroku open
    

    Your Python web app should now be live on Heroku!

Conclusion

Congratulations! You have learned how to deploy a Python web application on Heroku. In this tutorial, you went through the process of creating a Heroku account, installing the Heroku CLI, setting up a Python web app, creating a Git repository, and deploying the app on Heroku. Now you can easily deploy and manage your Python web apps on Heroku. Happy coding!