Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to deploy python modules on Heroku?
Heroku is a cloud platform that makes it easy to deploy Python applications. This guide walks you through deploying a Python module to Heroku using Git and the Heroku CLI.
Prerequisites
Before deploying to Heroku, ensure you have −
- Python 3.6+ installed locally
- Pipenv or pip for dependency management
- Heroku CLI installed and logged in
- Git repository for your application
Follow the official Heroku Python setup guide for initial configuration.
Creating a Heroku Application
Navigate to your project's root directory and create a new Heroku application ?
$ heroku create Creating lit-bastion-5032 in organization heroku... done, stack is cedar-14 https://lit-bastion-5032.herokuapp.com/ | https://git.heroku.com/lit-bastion-5032.git Git remote heroku added
Heroku generates a random name (like lit-bastion-5032) for your app and adds a git remote called heroku to your local repository. You can specify a custom name using heroku create your-app-name.
Required Files
Your Python application needs specific files for Heroku deployment ?
requirements.txt
Create a requirements.txt file listing all dependencies with version numbers ?
Flask==2.3.2 Jinja2==3.1.2 Werkzeug==2.3.6 requests==2.31.0 gunicorn==20.1.0
Procfile
Create a Procfile to specify how Heroku should run your application ?
web: gunicorn app:app
Deploying to Heroku
Push your code to Heroku using Git ?
$ git push heroku master Counting objects: 232, done. Delta compression using up to 4 threads. Compressing objects: 100% (217/217), done. Writing objects: 100% (232/232), 29.64 KiB | 0 bytes/s, done. Total 232 (delta 118), reused 0 (delta 0) remote: Compressing source files... done. remote: Building source: remote: remote: -----> Python app detected remote: -----> Installing python-3.6.0 remote: -----> Installing requirements with latest pipenv... remote: Installing dependencies from Pipfile.lock... remote: $ python manage.py collectstatic --noinput remote: 58 static files copied to '/app/gettingstarted/staticfiles', 58 post-processed. remote: remote: -----> Discovering process types remote: Procfile declares types -> web remote: remote: -----> Compressing... remote: Done: 39.3M remote: -----> Launching... remote: Released v4 remote: http://lit-bastion-5032.herokuapp.com/ deployed to Heroku remote: remote: Verifying deploy... done. To git@heroku.com:lit-bastion-5032.git * [new branch] master -> master
Key Deployment Steps
| Step | Command | Purpose |
|---|---|---|
| Create App | heroku create |
Sets up Heroku application |
| Add Files | requirements.txt, Procfile | Defines dependencies and startup |
| Deploy | git push heroku master |
Uploads and builds application |
| View | heroku open |
Opens deployed application |
Conclusion
Deploying Python modules to Heroku requires a git repository, proper dependency specification in requirements.txt, and a Procfile. Once configured, deployment is as simple as pushing to the Heroku git remote.
