Managing Dependencies with requirements.txt

Tutorial 4 of 5

1. Introduction

In this tutorial, we are going to learn about managing dependencies in a Flask project using a requirements.txt file. Dependencies are external packages that your project uses. Managing these dependencies is crucial to ensure that your project runs smoothly across all environments.

By the end of this tutorial, you will learn:
- What is a requirements.txt file and why it is important
- How to generate a requirements.txt file
- How to use the requirements.txt file to install dependencies in a new environment

Prerequisites:
- Basic knowledge of Python
- A working installation of Python and pip on your computer

2. Step-by-Step Guide

A requirements.txt file is a file that contains a list of items to be installed using pip install command. Here's how to use it:

  • Generating a requirements.txt file

In your terminal, navigate to the project directory and type the following command:

pip freeze > requirements.txt

This command will generate a requirements.txt file in your project directory.

  • Using the requirements.txt file to install dependencies

To install the dependencies listed in the requirements.txt file, navigate to the project directory and type the following command:

pip install -r requirements.txt

3. Code Examples

  • Example 1: Generating a requirements.txt file

Suppose you have a Flask project with the following dependencies: Flask, Flask-SQLAlchemy, and Flask-Migrate. You can generate a requirements.txt file as follows:

bash $ pip freeze > requirements.txt

The requirements.txt file will look something like this:

Flask==1.1.2 Flask-SQLAlchemy==2.4.4 Flask-Migrate==2.5.3

  • Example 2: Using the requirements.txt file to install dependencies

To install the dependencies listed in the requirements.txt file, you can use the following command:

bash $ pip install -r requirements.txt

This will install Flask, Flask-SQLAlchemy, and Flask-Migrate in your Python environment.

4. Summary

In this tutorial, we learned about managing dependencies in a Flask project using a requirements.txt file. We learned how to generate this file and how to use it to install dependencies in a new Python environment.

Next, you might want to learn about virtual environments in Python, which are a more robust way to manage dependencies in your projects.

5. Practice Exercises

  • Exercise 1: Create a new Flask project and add a few dependencies. Generate a requirements.txt file and inspect its contents.
  • Exercise 2: Delete your Python environment and create a new one. Use the requirements.txt file to reinstall your dependencies.

Solutions

  • Solution 1: You should see a list of your dependencies in the requirements.txt file.
  • Solution 2: After reinstalling your dependencies, your Flask project should work exactly as it did before you deleted your Python environment.

Keep practicing this until you're comfortable with managing dependencies using a requirements.txt file, as it is a key skill in Python web development.