Stop Saying "It Works on My Machine!"
Ever tried to share your Python project and had someone tell you, "It doesn't work on my machine?" That usually happens because they're missing a library or using the wrong version.
The fix is simple: the requirements.txt file.
Think of it as the master list or shopping list for your project. It tells anyone exactly which libraries and versions they need to install to make your code run perfectly.
Here’s the fastest way to generate this file on Windows, using the modern and blazing-fast uv tool.
Why Bother Making This File?
It might seem like an extra step, but creating this file saves you massive headaches later on. It all comes down to control and consistency.
1. It Stops Breakages (Version Control)
Libraries are constantly being updated. A new version might change something small that totally breaks your existing code. If you rely on someone else installing the latest version, your project could stop working overnight! By pinning the exact version in requirements.txt (like requests==2.31.0), you ensure your project only ever uses the version you tested it with.
2. It Makes Sharing Easy
Imagine sharing a complex recipe. You don't just say, "Get flour, sugar, and butter." You list the exact amounts. Similarly, you don't share your code and say, "You'll need numpy." You share the requirements.txt file. This lets other developers (or your future self!) set up the environment perfectly with just one command.
3. It Keeps Your Environment Clean
When you delete and recreate your virtual environment, this file is the key to immediately rebuilding it exactly how it was before. It makes the entire environment disposable and easy to manage.
Step 1: Get Into Your Workspace
Before we do anything, you need to be inside your project's isolated environment—that's your .venv folder! This ensures we only list the packages specific to your project, not every random library on your computer.
- Open your project folder in your terminal (like PowerShell or Command Prompt).
- Run the command to activate your environment. You'll know it works when your terminal prompt starts with
(.venv).
- For PowerShell:
.venv\Scripts\Activate.ps1
Step 2: Install the Stuff You Need
This is where you install any libraries your project uses. If you've already installed them, great! If not, install them now using uv:
-
Example: Install the popular
requestslibrary:
(.venv) $ uv pip install requests
Step 3: Generate the File (The Magic Command!)
This is the key step. We're going to use the uv pip freeze command to inspect your environment and then use the magical redirect symbol (>) to write that list straight into a file.
- Make sure you're in your project's main folder and the environment is active.
- Run this single line:
(.venv) $ uv pip freeze > requirements.txt
What Just Happened?
uv pip freeze: This command quickly scans your active environment and prints a neat list of every installed package, locking in its exact version (likerequests==2.31.0).>: This little symbol is crucial. It tells your computer, "Take the output from the left side and save it into the file named on the right side."
Step 4: You're Done!
- Check your project folder—you now have a clean
requirements.txtfile! - If you open it, you’ll see the pinned versions of your dependencies.
Now what?
When you share your project, the next person just needs to run one simple command after creating their own environment:
uv pip install -r requirements.txt
Note: This blog was AI generated.
Top comments (0)