Here is how to deploy a web app to Heroku in Python.


Here are the steps to deploy a Python web app to Heroku using Git and Github:

Requirements:

- You should have Git installed on your computer.

- You should have a Github account.

- You should have a free or paid Heroku account.

Steps:

1. Go to your IDE project where you have your web app files and create a requirements.txt file by running the following command in your terminal:

pip freeze > requirements.txt

That will inject all the names of the installed Python packages of your project in the requirements.txt file. The Heroku server needs that file to know what packages to install.

2. Create an empty file in your project root directory. This is the same directory where the requirements.txt file was created. Name the file Procfile. Note that this should be a plain file without any extension, It should not even have a .txt extension and the filename should start with a capital P. Once you create the Procfile, type in the following text to the file:

web: python main.py

Note: The above text could be different in your case. This one assumes that you run your web app using  python main.py in your local IDE. So, we are telling the server to run main.py using the python command. This is usually the case for Flask apps. Depending on what web app you are deploying your command might be different and the name of the Python file which you execute locally in your IDE might also be different. In my case the file was named main.py. If you are deploying a Django app, the command might be python run main.py

3. Set up Git for your project following the steps below:

   a. Open your Python project on your IDE. Then open your IDE terminal and run the following command to enable git for your project:

        git init .

  b. Add files to git and commit:

        git add .

        git commit -m "Initial commit"

4. Create a repository on Github and copy the link of the repository on your clipboard.

5. Associate the Github repository with your local project by running the following on your IDE terminal (Note: Replace the URL with the URL of your Github repository):

  git add origin https://github.com/etc/etc

6. Upload the IDE project files to the Github repository by running the following on your IDE terminal:

  git push origin master

7. Go to your Heroku account and create a new Heroku app.

8. Once you create the app, choose the Github option in the next page. You may be asked to provide your Github account credentials.


9 Once you connect Github with Heroku, search for the repository name you created in Step 2 and connect that repository with Heroku using the Connect button.

10. Once you connect the repository, click on Enable Automatic Deploys. Enabling this will update the live app whenever the Github repository code is updated when you use execute the "git push" command.


11. Go to the end of the page and click the Deploy Branch button to finalize the deployment.


Output

You will be able to visit the webpage at your_app_name.herokuapp.com.


Explanation

In a nutshell, the above process consists of three phases. First, we upload our project files to Github. Then, we download the uploaded files from Github to Heroku and Heroku runs those files to generate the web app.



Related HowTos
Use the polars library
Use Selenium to browse a page
Load JSON data
Build a GUI with FreeSimpleGUI
Delete a directory
Delete a file
Create matplotlib graphs
Rename a file
Get today's date and convert to string
Create a text file and write text on it
Read a text file with Python
Scrape a Wikipedia page
Install dependencies
Flush the output of the print function
Prettyprint a JSON file
Create a dictionary with list comprehension
Select multiple columns in a Pandas dataframe
Profile a script
Reverse a string
Convert two lists into a dictionary
Convert an integer into a string
Generate random strings with upper case letters and digits
Extract extension from filename
Print to stderr in Python
Generate random integers between 0 and 9
Use a pythonic way to create a long multi-line string
Measure elapsed time in Python
Install specific package versions with pip
Read from stdin (standard input)
Get the class name of an instance
Check if a string is empty
Pad zeroes to a string
Delete an element from a dictionary
Check if a string is a float
Count the occurrences of an item in a list
Remove an element from a list by index
Use static methods
Remove a trailing newline
Print literal curly-brace characters in a string and also use .format on it
Exit/deactivate a virtualenv
Determine the type of an object
Limit floats to two decimal points
Know if an object has an attribute
Select an item from a list randomly
Read a file line-by-line into a list
Call a function of a module by using its name
Get the number of elements in a list
Print without a newline or space
Sort a list of dictionaries by a value of the dictionary
Remove a key from dictionary
Rename column names with Pandas
Lowercase a string
Upgrade all Python packages with pip
Get a substring of a string
Get the last element of a list
Parse a string to a float or integer
Convert a string into datetime format
Access environment variable values
Print coloured text on the terminal
Find current directory of a file
Change the size of figures drawn with Matplotlib
Manually raise an exception
Delete a file or folder
Split a list into evenly sized parts
Select rows from a DataFrame based on column values with Pandas
Install pip on Windows
Check if a given key already exists in a dictionary
Iterate over rows in a DataFrame for Pandas
Make function decorators and chain them together
Pass a variable by reference
Make a time delay
Convert bytes to a string
Copy a file
Concatenate two lists
Add new keys to a dictionary
Catch multiple exceptions in a single 'except' block
Check if a list is empty
Get the current time
Sort a dictionary by value
Use global variables in a function
List all files of a directory
Iterate over dictionaries using for loops
Check if a string contains a substring
Find the index of an item in a list
Understand how slice notation works
Make a flat list out of a list of lists
Accesses the index in for loops
Safely create a nested directory
Merge two dictionaries in a single expression
Check whether a file exists without exceptions
Convert a dictionary into a list
Convert a list into a dictionary
Duplicate a file
Append text in a text file
Use for loops
Schedule a Python script for execution at a specific time every day
Store Python passwords securely on Windows, Mac, and Linux
Do dictionary comprehension
Do list comprehension
Create a virtual environment
Create a new file
Merge two lists
Extract items from two different lists into one list
Check if a text file is empty
Randomly select an item from a list
Generate a random integer
Break a while loop
Create a pandas DataFrame from a dictionary
Create a pandas DataFrame from a list
Get the last item of a list
Delete a column from a pandas dataframe
Access a column of a pandas dataframe
Create a class
Make a webpage request
Get the first two characters of a string
Loop through two lists at the same time
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.