Table of Contents
- Introduction
- Prerequisites
- Setup
- Creating a Heroku Account
- Installing the Heroku CLI
- Creating a Python Web App
- Setting Up a Git Repository
- Deploying the App on Heroku
- 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
- Open your web browser and go to the Heroku website.
- Click on the “Sign Up” button to create a new account.
- Fill in the required information and click on the “Create Account” button.
- Check your email for a verification link and follow the instructions to verify your account.
Installing the Heroku CLI
- Open a terminal or command prompt on your computer.
-
Run the following command to install the Heroku CLI:
$ curl https://cli-assets.heroku.com/install.sh | sh
-
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
- Create a new directory for your Python web app project.
- Open a terminal or command prompt and navigate to the project directory.
-
Run the following command to create a virtual environment:
$ python -m venv env
-
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.
-
Initialize a new Git repository in your project directory:
$ git init
-
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
-
Save the file as
.gitignore
in your project directory. -
Add all the files in your project directory to the Git repository:
$ git add .
-
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.
-
Login to Heroku from the command line:
$ heroku login
-
Enter your Heroku credentials when prompted.
-
Create a new Heroku app:
$ heroku create
-
Verify that a new remote repository named “heroku” has been added to your Git configuration:
$ git remote -v
-
Deploy your app to Heroku:
$ git push heroku master
-
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!