Python
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax and Data Types
-
Section 3: Collections
-
39: Set Operations: Union, Intersection, Difference
-
Section 4: Control Flow
-
Section 5: Functions
-
Section 6: Turtle Graphics and Early Practice Projects
-
Section 7: Working with Files and I/O
-
Section 8: Regular Expressions
-
Section 9: Object-Oriented Python
-
Section 10: Error Handling
-
Section 11: Modules and Packages
-
Section 12: Iterators, Generators, and Functional Tools
-
Section 13: Decorators and Metaprogramming
-
Section 14: Concurrency and Parallelism
-
Section 15: Working with Dates, Times, and Numbers
-
Section 16: Standard Library Deep Dive I: Data Structures
-
Section 17: Standard Library Deep Dive II: System and Introspection
-
Section 18: Standard Library Deep Dive III: Security and Encoding
-
Section 19: Standard Library Deep Dive IV: Text and Data Utilities
-
Section 20: Networking and Web Basics
-
Section 21: Working with Databases
-
Section 22: Testing and Quality
-
Section 23: Advanced Typing
-
Section 24: Context Managers and Resource Handling
-
Section 25: Text, Unicode, and Binary Data
-
Section 26: More Functional and Iteration Tools
-
Section 27: Data Validation and Configuration
-
Section 28: Working with Images and Media
-
Section 29: Property-Based and Documentation Testing
-
Section 30: Packaging and Deployment
-
Section 31: Performance and Internals
-
Section 32: Design Patterns in Python
-
Section 33: GUI Programming
-
Section 34: Security Basics
-
Section 35: Data Structures and Algorithms
-
Section 36: Practical Projects
-
Section 37: Capstone Projects
-
Section 38: Interview and Algorithm Practice
-
Section 39: Writing Idiomatic Python
164: Using requirements.txt
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 becauserequestsis 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.txtfile. This is just a plain text file that lists every package your project needs to run. I usually generate this automatically using thefreezecommand.Run this in your terminal while your project is working:
pip freeze > requirements.txtThe
>operator tells the terminal to take the output ofpip 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.4Notice the
==. This is "pinning" the version. I highly recommend doing this. If you just putrequestsin 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.txtfile 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.txtThe
-rflag 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:
- Create a new directory for this project and set up a virtual environment.
- Install
pandasandmatplotlibusing pip. - Create a file named
tracker.pyand 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!") - Run the script to confirm it works.
- Generate a
requirements.txtfile using thepip freezecommand. - Verify that
pandasandmatplotlib(and their dependencies) are listed in the text file with their specific version numbers.
There are no comments for now.