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
163: Managing Dependencies with pip
Imagine you've just cloned a promising project from GitHub or a teammate just sent you a folder containing a script they've been working on. You've got your environment ready, you run the script, and you're immediately hit with this:
Traceback (most recent call last):
File "data_processor.py", line 1, in <module>
import requests
ModuleNotFoundError: No module named 'requests'
You look at the code, and line 1 is exactly that: import requests. The code is correct, your Python installation is fine, but the program is crashing. This is the most common "broken" state a Python developer encounters when moving code between machines. The problem isn't your code; it's that your environment is missing a dependency.
The missing piece: External Packages
Python comes with a "Standard Library"—a bunch of modules like math or datetime that are always there. But for things like making HTTP requests, handling complex dataframes, or building web servers, we rely on the community. The requests library is a perfect example; it's the industry standard for API calls, but it doesn't come pre-installed with Python.
To fix this immediately, you need pip. If you're on a modern Python installation, you already have it. It's the package manager that reaches out to the Python Package Index (PyPI), downloads the library, and installs it into your environment.
# Run this in your terminal, not in the Python interpreter
pip install requests
Once that finishes, you can run data_processor.py again, and that ModuleNotFoundError will vanish. But here is the reality: in a professional project, you might have 20, 50, or 100 dependencies. You can't expect your teammates to play a guessing game, installing packages one by one every time they hit an error.
Scaling up with requirements.txt
I've seen too many junior devs send a ZIP file of their code and a separate text email saying, "Oh, by the way, you'll need to install pandas, numpy, and scikit-learn." That's a recipe for version conflicts and wasted time. The professional way to handle this is a requirements.txt file.
This is a simple text file located in the root of your project that lists every single package your project needs to run. It looks like this:
requests==2.31.0
pandas>=2.0.0
numpy==1.26.0
The == ensures everyone uses the exact same version, which prevents the "it works on my machine" syndrome where a newer version of a library breaks an old function. Now, when a new developer joins the project, they don't install things one by one. They run one command:
pip install -r requirements.txt
The -r flag tells pip to "read" the file and install everything listed inside. It's clean, it's fast, and it's predictable.
Capturing your environment with pip freeze
You might be wondering: "Do I have to write this text file by hand?" Absolutely not. That would be tedious and prone to typos.
If you've been installing packages as you build your project and everything is working perfectly, you can use pip freeze. This command outputs every package currently installed in your environment along with its exact version.
To save this directly into a file, use the redirection operator in your terminal:
pip freeze > requirements.txt
I usually do this right before I commit my code to Git. It ensures that the snapshot of my working environment is preserved for whoever pulls the code next. Just a word of caution: pip freeze captures everything in your current environment. This is why, in future lessons, we'll talk about virtual environments—so you don't accidentally include a random library you installed for a different project three months ago in your current project's requirements.
📋 Practical Task
Project: Building a Currency Converter with Dependency Locking
Your goal is to create a small project that relies on an external library and properly documents its dependencies so it can be replicated on another machine.
- Install the library: Open your terminal and install the
forex-pythonlibrary (orcurrencyconverterif you prefer) usingpip. - Write the script: Create a file named
converter.py. Write a short script that imports the library and prints the current exchange rate between two currencies (e.g., USD to EUR). - Verify the run: Execute the script to ensure it works and the library is functioning.
- Generate the lockfile: Use the
pip freezecommand to create arequirements.txtfile containing the necessary dependencies for this script. - Verification check: Open your
requirements.txtfile and ensure the library you installed is listed there with a specific version number.
There are no comments for now.