Pipenv to Heroku: Easy App Deployment | by Edward Krueger | Towards Data Science

By: Edward Krueger Data Scientist and Instructor and Douglas Franklin Teaching Assistant and Technical Writer.
In this article, we will cover deploying an app with a Pipfile from a Github repository to make app deployment easy!
Data scientists are often interdisciplinary and have not been taught to work collaboratively with others and push projects into production. Hence deployment, and proper environment and package management skills are often lacking. This creates difficulty reproducing, deploying and sharing projects. Reproducible data science projects are those that allow others to recreate and build upon your analysis and to reuse and modify your code easily.
Newer developers often install everything at the system level do to a lack of understanding of, or experience with, virtual environments. Packages installed with pip are placed at the system level. The result of doing this for every project is a bloated and unmanageable singular Python environment. How can someone developing an app in this environment know what dependencies to include in their requrements.txt?
Data scientists need to be able to put their models into production. Using good practices during the model development greatly simplifies the deployment process. Even Data scientists with separate deployment or development teams can benefit from learning the deployment process. The Data scientist who is aware of the complete development to deployment workflow can deliver a product that is much more ready for deployment. This awareness can take some strain off of developers and DevOps teams.
Effective environment management saves time and allows developers and data scientists to create isolated software products that are easy to deploy.
Pipenv combines package management and virtual environment control into one tool for installing, removing, tracking, and documenting your dependencies; and to create, use, and manage your virtual environments. Pipenv is essentially pip and virtualenv wrapped together into a single product.
Heroku offers many software service products. We’ll need the Heroku cloud platform service to host apps. Don’t worry; creating an account and hosting an app is free. The cloud platform supports apps in many programming languages including, Python, Node.js, Scala, Ruby, Clojure, Java, PHP and Go.
Once we have an app with a Pipfile pushed to GitHub, Heroku allows us to deploy an app from Github to Heroku quickly. Be sure to have your Pipfile at the project’s root directory. Our app uses an SQLite database for this deployment.
We’ll need to make a couple of changes to our app project files so that the app can run on Heroku.
Gunicorn is a Python WSGI HTTP server that will serve your Flask application on Heroku. By running the line below, you add gunicorn to your Pipfile, which is needed to run your app in Heroku’s containers.
pipenv install gunicorn
Create a Procfile in the project root folder and add the following line:
web: gunicorn app:app
The first app represents the name of the python file that runs your application or the name of the module where the app is located. The second app represents your app name, i.e., app.py. This Procfile works with gunicorn and Heroku's Dynos to serve your app remotely.
Once we have our app tested and working locally, we push all code to the master branch. Then on Heroku, go to deploy a new app to see the page below.
Next on Heroku, select GitHub and enter the name of the repository and hit search. Once your Username/repository appears, click connect. Select the desired branch and click deploy.
Build logs will begin to populate a console on the page. Notice that Heroku looks for a requirements.txt file first then installs dependencies from Pipenv’s Pipfile.lock.
Once your environment has been built from the Pipfile.lock and the build is successful, you will see the below message.
The app is successfully deployed! Click to view button to see the deployed app on Heroku.
We can enable automatic deployment to have changes to the Github master be displayed on Heroku as they are pushed. If you use this method, you’ll want to be sure that you always have a working master branch.
Practicing good environment and package management is crucial for data scientists and developers who want their code deployed, built upon, or used in production. These development skills can help teams when used by data scientists and developers upstream in their workflow.
Having a well managed Pipenv and Pipfile allowed Heroku’s severs to rebuild our app with minimal troubleshooting. This allowed us to take an app project directory from GitHub to a working published app in minutes.
Using an environment and package manager such as Pipenv makes many processes, including deployment, comfortable and more efficient! For more information on virtual environments or getting started with Pipenv, check out this article. We hope this guide has been helpful. Thank you!