Last modified: May 28, 2025 By Alexander Williams

Install Python Package in Heroku

Heroku is a popular cloud platform for deploying apps. It supports Python apps with ease. This guide shows how to install Python packages in Heroku.

Prerequisites

Before starting, ensure you have these:

- A Heroku account. Sign up if you don't have one.

- Heroku CLI installed on your machine.

- Git installed for version control.

- A Python app ready for deployment.

Step 1: Create a requirements.txt File

Heroku uses requirements.txt to install Python packages. List all dependencies here.

Create the file in your project root. Add packages line by line.


# Example requirements.txt
flask==2.0.1
requests==2.26.0
pandas==1.3.3

Save the file. Heroku will read it during deployment.

Step 2: Set Up Heroku CLI

Install the Heroku CLI if you haven't. Then log in using the terminal.


heroku login

Follow the prompts to log in. This links your local machine to Heroku.

Step 3: Create a Heroku App

Navigate to your project folder. Create a Heroku app.


heroku create your-app-name

Replace your-app-name with your desired name. Heroku will generate a URL.

Step 4: Deploy Your App

Push your code to Heroku using Git. Heroku will install packages automatically.


git push heroku master

Wait for the deployment to finish. Check the logs for errors.

Step 5: Verify Package Installation

After deployment, verify the packages installed correctly.


heroku run bash
pip freeze

This lists all installed packages. Compare it with your requirements.txt.

Using runtime.txt for Python Version

Specify a Python version with runtime.txt. Create it in your project root.


python-3.9.7

Heroku will use this version. Omit it to use the default.

Handling Build Failures

If builds fail, check the logs.


heroku logs --tail

Common issues include missing packages or version conflicts. Update requirements.txt accordingly.

Alternative Methods

For complex apps, consider Pipenv or Poetry. These tools manage dependencies better.

For other platforms, see guides on AWS Lambda or Google Colab.

Conclusion

Installing Python packages in Heroku is simple. Use requirements.txt for dependencies. Deploy with Git and monitor logs for errors.

Heroku handles the rest, making deployment smooth. Now your Python app is live with all required packages.