Skip to main content

How to Create a requirements.txt File for Python Apps? (The Beginner's Guide!)

Hi Guys let's start with our new blog. Alright, so you're working on a Python project, and you need to install a bunch of packages. But wait—how do you keep track of all of them? Do you write them down on a sticky note? Nah, that's too messy. That's where the requirements.txt file comes in! It helps you list all the necessary dependencies so you (or anyone else) can install them quickly and painlessly.

In this super-detailed, slightly chaotic guide, we’ll walk through everything you need to know about How to Create a requirements.txt File for Python Apps? — yes, even the mistakes you're likely to make. Let’s go!

What Even Is a requirements.txt File?

Think of requirements.txt as your shopping list for Python packages. Instead of remembering what to install manually, you just throw everything into this file, and BOOM—you’re good to go.

Here's what it typically looks like:

numpy==1.24.1
opencv-python==4.5.5.62
pandas==1.5.3
scikit-learn==1.2.2

See those double equal signs (==)? They lock in the exact package version, so there are no “works on my machine” disasters.

Why Bother With requirements.txt?

You could install everything manually, but why would you? Here’s why requirements.txt makes life easier:

1. Saves Time

Running pip install -r requirements.txt is way faster than installing each package one by one.

2. Avoids Compatibility Issues

Have you ever installed a package, and suddenly your entire project stopped working? That’s why you pin versions—so everything stays consistent.

3. Makes Sharing Easier

If you’re working on a team or sharing code, your teammates won’t have to guess what packages to install.

4. Deploy Like a Pro

If you're deploying a Python app to a server, requirements.txt is a lifesaver. The server reads the file and installs everything automatically.

Creating a requirements.txt File (The Easy Way)

Method 1: Manually Writing It

If you know what packages you need, just create a plain text file named requirements.txt and list them:

django==4.2.3
requests==2.28.1
flask==2.2.2

Yes, it’s really that simple. Just don’t forget the file extension (.txt).

Method 2: Generating It Automatically

Too lazy to type? No worries. Just run:

pip freeze > requirements.txt

This will scan your installed packages and dump them into requirements.txt. Then, you can check the file by running:

cat requirements.txt  # For Mac/Linux
type requirements.txt  # For Windows

Boom! Instant package list.

Installing Packages From requirements.txt

So, let’s say you have a fresh machine and need to install everything. Just run:

pip install -r requirements.txt

Need admin permissions? Try:

pip install --user -r requirements.txt

Using a virtual environment? Activate it first:

source venv/bin/activate  # Mac/Linux
venv\Scripts\activate  # Windows
pip install -r requirements.txt

Easy, right?

Pro Tips for Using requirements.txt

1. Always Pin Your Versions

You don’t want surprises, so lock versions like this:

numpy==1.24.1

2. Keep It Updated

Packages get updated all the time. Run:

pip freeze > requirements.txt

Every few weeks to keep things fresh.

3. Use Virtual Environments

A virtual environment keeps your project’s dependencies separate from system-wide Python packages. Always activate it before installing requirements.

4. Test After Installation

After installing, test your project to make sure nothing broke.

Common Errors (And How to Fix Them)

1. No module named pip

Fix: Install pip using:

python -m ensurepip --default-pip

2. Could not find a version that satisfies the requirement

Fix: Ensure you typed the package name correctly and update pip:

pip install --upgrade pip

3. Permission denied

Fix: Use the --user flag:

pip install --user -r requirements.txt

Wrapping Up

And that’s it! Now you know how to create and use a requirements.txt file like a pro (or at least like a very competent beginner). It’s a small but powerful tool that will save you headaches when managing dependencies.

Before you go, here’s a quick recap:

  • Creating it manually: Just type the package names and versions in a .txt file.
  • Generating it automatically: Use pip freeze > requirements.txt.
  • Installing from it: Run pip install -r requirements.txt.
  • Best practices: Pin versions, update it regularly, and always use virtual environments.

Now, go forth and manage your Python dependencies like a professional python developer. 

Comments