Skip to Content
Course content

163: Managing Dependencies with pip

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

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.

  1. Install the library: Open your terminal and install the forex-python library (or currencyconverter if you prefer) using pip.
  2. 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).
  3. Verify the run: Execute the script to ensure it works and the library is functioning.
  4. Generate the lockfile: Use the pip freeze command to create a requirements.txt file containing the necessary dependencies for this script.
  5. Verification check: Open your requirements.txt file and ensure the library you installed is listed there with a specific version number.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.