Here are the steps to deploy a Python web app to Heroku using Git and Github:
- You should have Git installed on your computer.
- You should have a Github account.
- You should have a free or paid Heroku account.
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.
You will be able to visit the webpage at your_app_name.herokuapp.com.
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.
Learn Flask development and learn to build cool apps with our premium Python course on Udemy.