Sharing is caring… Especially with code

Python requirements.txt: Managing Project Dependencies

Written by

In the previous article, we looked at Python virtual environments and why I prefer keeping each project isolated from the others. But creating a virtual environment is only part of the process. Eventually, you need a way to keep track of which packages your project requires.

This is where requirements.txt comes in.

Why Do We Need a requirements.txt File?

Imagine you’ve been working on a Python project for several months. You’ve installed packages along the way, updated a few things, and your application is running perfectly.

Now you want to move that project to another computer, recreate your development environment, or have someone else work on it.

How do you know which packages you installed?

You could try to remember them, but that’s not exactly a reliable development strategy.

A requirements.txt file provides a list of packages your project needs, optionally including their version numbers. This makes it much easier to recreate your environment without manually figuring out which dependencies are missing.

For example:

SQLAlchemy==2.0.44
requests==2.32.5
Flask==3.1.2

The version numbers are pinned here, meaning that we’re specifying the exact versions we want installed rather than allowing pip to install a different release.

Creating a requirements.txt File

If you’re starting a new project, you can create a file named requirements.txt in your project directory and add the packages your application requires.

However, if you’re working on an existing project and aren’t sure which packages it uses, there are a couple of ways to generate a requirements file.

Using pip freeze

One of the simplest methods is using Python’s built-in pip command.

With your virtual environment activated, run:

python -m pip freeze > requirements.txt

This will generate a requirements file containing the installed packages and their versions in your active environment.

The advantage is that you get a snapshot of what’s installed, making it easy to recreate that environment later.

The downside is that it doesn’t distinguish between packages you intentionally installed and packages installed automatically as dependencies. It may also include packages that aren’t actually needed by your project.

If you’ve been experimenting or installing packages along the way, your requirements file might contain more than your application needs.

Using pipreqs

Another option I recently used was pipreqs.

Unlike pip freeze, pipreqs examines your Python source code and attempts to identify the packages your project imports. It then generates a requirements file based on those imports.

You can install it using:

python -m pip install pipreqs

You can then run it from your project directory:

pipreqs . --force

The --force argument tells pipreqs to overwrite an existing requirements file if one is present.

This can be particularly useful when working with an existing project that has accumulated packages over time. Instead of trying to determine which packages are still necessary, pipreqs gives you a starting point based on what it finds in your source code.

However, it isn’t perfect. Python import names don’t always match the names of the packages you install, and dynamically imported modules or dependencies that aren’t directly referenced in your source code may not be detected.

For example, you might have code that imports:

import psycopg2

But the package you installed may be:

python -m pip install psycopg2-binary

The import name and package name aren’t always the same, so it’s important to review the generated file rather than blindly trusting it.

pip freeze vs. pipreqs

Both tools can help generate a requirements file, but they approach the problem differently.

pip freeze reports installed packages and their versions from the active environment. It provides a snapshot of that environment, including dependencies that other packages require.

pipreqs scans your source code to identify imports and attempts to determine which packages your project directly uses.

Neither approach is perfect for every situation. I like having both available, particularly when working with an existing project or trying to clean up its dependencies.

Installing Packages from requirements.txt

Once your requirements file is ready, recreating your environment becomes much easier.

First, create and activate a virtual environment if you haven’t already. If you need a refresher, see my previous article on Python virtual environments.

Then, instead of installing packages individually, run:

python -m pip install -r requirements.txt

Pip will read the file and install the listed packages along with their required dependencies.

This is particularly useful when moving projects between computers, setting up a development environment, or recovering a project after reinstalling Python.

Keeping Your Dependencies Organized

As your projects grow, keeping track of dependencies becomes increasingly important.

I prefer keeping my requirements file inside the project directory and updating it when I add, remove, or change packages. It also makes it easier to understand what a project needs without having to inspect the entire environment.

A requirements file isn’t necessarily a complete lockfile or guarantee of identical environments across every operating system and Python version, but it is a useful step toward making your development environment more predictable and easier to maintain.

Final Thoughts

Python virtual environments help isolate your projects, while requirements files help document and recreate the packages those projects depend on.

Together, they make managing Python projects much easier, particularly when you’re working on multiple applications that may require different package versions.

It’s a small step that can save a considerable amount of time when you need to rebuild an environment or return to a project months later.

Leave a Reply

Your email address will not be published. Required fields are marked *