Python > Deployment and Distribution > Deployment Platforms > Platform as a Service (PaaS) (e.g., Heroku, PythonAnywhere)
Deploying a Simple Flask App to Heroku
This snippet demonstrates deploying a basic Flask application to Heroku, a popular Platform as a Service (PaaS). It covers the necessary steps, including creating a `Procfile`, `requirements.txt`, and configuring the application for Heroku's environment.
Prerequisites
Before you begin, make sure you have the following installed: * **Python:** Python 3.6 or later is recommended. * **Pip:** Python package installer. * **Git:** Version control system. * **Heroku CLI:** Heroku command-line interface. You can download it from [https://devcenter.heroku.com/articles/heroku-cli](https://devcenter.heroku.com/articles/heroku-cli). * **Heroku Account:** You'll need a Heroku account. You can sign up for free at [https://www.heroku.com/](https://www.heroku.com/).
Create a Basic Flask Application
This code creates a simple Flask application with a single route that returns 'Hello, World!'. Save this as `app.py`.
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello, World!'
if __name__ == '__main__':
app.run(debug=True)
Create `requirements.txt`
This file lists the Python packages your application depends on. Heroku uses this file to install the dependencies. Create a file named `requirements.txt` and add `Flask` to it. For more complex applications, use `pip freeze > requirements.txt` to automatically generate the file from your virtual environment.
Flask
Create `Procfile`
The `Procfile` specifies the command that Heroku uses to start your application. Create a file named `Procfile` (without any extension) and add the above line. This tells Heroku to use `gunicorn` (a WSGI server) to run your Flask application. Make sure you install gunicorn if you don't have it already: `pip install gunicorn`.
web: gunicorn app:app
Initialize Git Repository
Initialize a Git repository in your project directory and commit your files.
git init
git add .
git commit -m "Initial commit"
Create a Heroku App
Use the Heroku CLI to create a new Heroku application. This command will automatically create a Git remote named `heroku`. Heroku will give your app a random name, but you can also specify a name like this: `heroku create my-awesome-app`.
heroku create
Deploy to Heroku
Push your code to the Heroku Git remote. Heroku will automatically detect the `requirements.txt` file, install the dependencies, and run your application.
git push heroku main
Open the Application
Open your application in a web browser using the Heroku CLI.
heroku open
Real-Life Use Case
This deployment process is suitable for simple web applications, APIs, or microservices. It's a quick way to get a Python application up and running without managing servers.
Best Practices
Pros
Cons
FAQ
-
What is gunicorn?
Gunicorn ('Green Unicorn') is a WSGI server that's commonly used to serve Python web applications. It's a production-ready server that's designed to be robust and efficient. -
Why do I need a `Procfile`?
The `Procfile` tells Heroku how to start your application. It specifies the command that should be executed when your application is deployed. -
How do I update my application?
To update your application, make changes to your code, commit them to Git, and then push them to Heroku using `git push heroku main`.