# Deploy the movie watchlist project to Heroku

# Files we need to add for Heroku deployment

There are three files we may need to change or add to our project:

  • Procfile, which tells Heroku what command to run
  • runtime.txt, which tells Heroku what Python version to use
  • requirements.txt, which we must make sure has all dependencies our project uses

# Writing the Procfile

web: gunicorn "movie_library:create_app()"

We'll be using gunicorn, which takes in as an argument the command to run to find the Flask app.

Here we're telling gunicorn that it should look in the movie_library module (i.e. movie_library.__init__.py), and in that file it should run the create_app() function. The return value of that function is the app.

# Writing the runtime.txt

To tell Heroku what Python version to use, we just add another single-line file called runtime.txt. Make sure to use the latest Python version[1] supported by Heroku, as new Python versions often add features, improvements, and potentially fix bugs and security issues.

At the time of recording, the latest supported Python version was Python 3.10.3, so that's what we'll write in runtime.txt:

python-3.10.3

# Verifying the requirements.txt

The easiest way to make sure your requirements.txt file has everything your app needs is to do the following:

  1. Deactivate your virtual environment
  2. Delete your virtual environment folder (mine is called .venv)
  3. Re-create your virtual environment folder, using Python 3.10.3, and activate it
  4. Install the requirements by running pip install -r requirements.txt (this is what Heroku will run when you deploy)
  5. Run your app and make sure it works

If your app works without any problems, then you've got everything you need!

# Config variables needed for Heroku

Our application uses MongoDB Atlas, so grab the connection string and add it as a Heroku Config Var.

This is under the Heroku project -> Settings -> Config Vars.

Make sure the Config Var you add has the correct name, depending on what you've used in __init__.py to access it from the environment variables.

In my case, that's MONGODB_URI.

# Putting the movie watchlist on GitHub

To deploy to Heroku you'll want to put your entire code in GitHub, if it isn't there already.

Create a new public (or private) GitHub repository and push to it all your project files.

You don't want to push:

  • .vscode
  • .venv (or any other virtual environment folder)
  • Hidden files such as .DS_Store
  • Compiled Python files, all files ending in .pyc
  • Python cache folders, all __pycache__ folders

Think carefully about what files you're adding to your repository. You only want to add those files which are necessary and cannot be created by whoever downloads your code.

For example, *.pyc and __pycache__ are generated by Python when you run the code. Therefore, it's not necessary to add them to the repository.

Similarly, .vscode stores workspace-specific Visual Studio Code settings. Whoever downloads your code will have their own settings, or they may not even be using VSCode!

The virtual environment folder should be created in their computer by each person who downloads the project. You shouldn't include it in your repository.

And so on! Be critical about what you add, and only add what's precisely necessary!

# Deploying on Heroku

Now that you've got the GitHub repo, all you have to do is create a Heroku project and link it to the GitHub repo.

Then you can hit "Deploy" (under the project -> Deploy, and scroll to the bottom), and you should see it become live shortly!


  1. Heroku Python Runtimes (Official Documentation) (opens new window) ↩ī¸Ž