Skip to Content
Course content

164: Using requirements.txt

Click on the "Edit" button in the top corner of the screen to edit your slide content.

I can't tell you how many times I've seen a junior dev send me a zip file of their project, only for me to spend twenty minutes playing "guess the library" because the code wouldn't run. It usually looks something like this:

# main.py
import requests
from bs4 import BeautifulSoup

def get_page_title(url):
    response = requests.get(url)
    soup = BeautifulSoup(response.text, 'html.parser')
    return soup.title.string

print(get_page_title("https://www.python.org"))

You run the code, and immediately you're hit with this:

Traceback (most recent call last):
  File "main.py", line 2, in <module>
    from bs4 import BeautifulSoup
ModuleNotFoundError: No module named 'bs4'


You think, "Okay, easy, I'll just pip install beautifulsoup4." But then you run it again, and it crashes because requests is missing. Then you install that, but wait—the script was written for an older version of a different library, and now you have a version conflict. This is a tedious, frustrating way to manage a project, and it's exactly why we use a requirements file.

The chaos of manual installation

The problem isn't that you don't know how to install packages; it's that your project's dependencies are living in your head (or your current environment) rather than in the code repository. When you move your project to a new computer, a server, or a teammate's machine, that environment is gone. If you have ten different libraries with specific version requirements, manually installing them one by one is a recipe for disaster.

Freezing your environment into a list

Instead of telling people "you'll need requests and bs4," we create a requirements.txt file. This is just a plain text file that lists every package your project needs to run. I usually generate this automatically using the freeze command.

Run this in your terminal while your project is working:

pip freeze > requirements.txt

The > operator tells the terminal to take the output of pip freeze (which is a list of every package installed in your current environment) and write it directly into a file. If you open that file, you'll see something like this:

beautifulsoup4==4.12.2
requests==2.31.0
certifi==2023.7.22
charset-normalizer==3.2.0
idna==3.4
urllib3==2.0.4

Notice the ==. This is "pinning" the version. I highly recommend doing this. If you just put requests in the file, pip will install the latest version. If the library authors release a breaking change tomorrow, your code will stop working. Pinning ensures that the code runs exactly the same way on every machine.

Installing dependencies in one shot

Now, when you share your project, you include that requirements.txt file in the root folder. When someone else (or you, on a new machine) clones the project, they don't have to guess what to install. They just run one command:

pip install -r requirements.txt

The -r flag stands for "requirement." Pip reads the file, finds every listed package and version, and installs them all in the correct order. It transforms a ten-minute debugging session into a five-second command.




📋 Practical Task

Building a Dependency Manifest for a Finance Tracker

You have been given a script for a basic Finance Tracker, but the original developer forgot to include a requirements file. The script relies on pandas for data manipulation and matplotlib for creating charts.

Your Task:

  1. Create a new directory for this project and set up a virtual environment.
  2. Install pandas and matplotlib using pip.
  3. Create a file named tracker.py and paste the following code into it:
    import pandas as pd
    import matplotlib.pyplot as plt
    
    data = {'Month': ['Jan', 'Feb', 'Mar'], 'Savings': [100, 150, 120]}
    df = pd.DataFrame(data)
    df.plot(x='Month', y='Savings', kind='bar')
    plt.show()
    print("Chart generated successfully!")
    
  4. Run the script to confirm it works.
  5. Generate a requirements.txt file using the pip freeze command.
  6. Verify that pandas and matplotlib (and their dependencies) are listed in the text file with their specific version numbers.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.