Using Requirement Files
A requirements file is a list of all of a project’s dependencies. This includes the dependencies needed by the dependencies. It also contains the specific version of each dependency, specified with a double equals sign (
pip freeze will list the current projects dependencies to
This shell command will export this as a file named
$ pip freeze > requirements.txt
Once you’ve got your requirements file, you can head over to a different computer or new virtual environment and run the following:
$ pip install -r requirements.txt
That’s assuming you are working in the directory containing
requirements.txt. This tells
pip to install the specific versions of all the dependencies listed.
By modifying the requirements file to use
>= instead of
==, you can tell
pip to install the latest stable version of the dependency, with the version specified acting as a minimum. This line would tell
pip to install the latest version of
requests, but never version 2.23.0:
requests>=2.22.0, != 2.23.0.
To upgrade your installed packages, run the following:
$ pip install --upgrade -r requirements.txt
00:08 But sometimes you might want to install a specific version that you know works well with your code. What we really want to do here is create a specification of the dependencies, including the specific versions that we used to develop our application. This way, other developers know exactly what they’ll need to run our program.
This sounds like a job for a requirements file. A requirements file lists all of a program’s dependencies. As you’ll see in a moment, we can pass this file into
pip and it will install all of them for us. Requirements for the current project can be listed to
Now we can quickly view that file with the Unix
cat utility, just to make sure everything looks right. You can then head over to a different computer or a new virtual environment and say
pip install -r requirements.txt to install every package listed in the file.
Just make sure your current directory contains this requirements file. You’ll often see this requirements file in public repositories on GitHub. Over time, packages get updated with new performance and security updates. In order to take advantage of them, let’s modify our new requirements file to instruct
pip to install the latest and greatest version of these packages.
We can change these to greater than or equal to signs (
>=) if we want to install the newest version. I’ll type
i to go into insert mode, and now I can insert the greater than symbol (
>) on all of these lines.
What we’re basically doing is telling
pip that this is the minimum required version of the package, but it should use something newer if it’s compatible with the rest of our packages, I’ll hit Escape to exit insert mode and then
:wq to save and quit. To tell
pip to try and upgrade the current packages with the new requirements, we can say
pip install --upgrade -r requirements.txt.
And as you can see by the output, everything is already up-to-date. Let’s pretend we run our program and we determined that one of the updates to one of our packages breaks everything. We need some way to specify that it should not use that specific version of the package. Let’s head back into the requirements file, and I’ll show you how. By putting a comma (
,) after a version number, we can specify more constraints.
The less than symbol (
<) can be used to say that we need a version number less than this version. If we’ve identified just a specific version that doesn’t work, we can also say not equals (
!=), like this. As you can see,
pip makes it easy to fine-tune packages’ dependencies.
Become a Member to join the conversation.