I am in my terminal here and I’ve got a virtual environment activated. So to show you that this is a completely new virtual environment where I haven’t installed any third-party dependencies, I am going to run the
pip list command.
So now I am going to install a couple of third-party packages so we can actually start creating a requirements file. Alright, I just installed the latest version of Requests and now, when I run
pip list again, you can see here that Requests shows up in this list.
So when I run the
pip freeze command now, I get a different result, it actually captured the Requests library as a third-party dependency, so the output of the
pip freeze command is all we need to create our
So this is the command I would run here, and once that finished, it created a new
requirements.txt file. So when I take a look at this file you can see that it contains exactly the output of the
pip freeze command, and that is a very quick way to capture the dependencies that are installed in your virtual environment or in any Python environment for that matter.
Now before we move on, I want to show you how
pip freeze, not only captures the top level dependencies, but it actually is smart enough to go and capture all of the secondary or so called transitive dependencies, to include them in its output which we could then put into
So Requests doesn’t actually have any third-party dependencies, so it doesn’t really make a great example here, so now I am going to install the Flask module, as another third-party dependency because Flask actually contains a bunch of secondary dependencies that I can then use to demo how
pip freeze deals with them.
Okay, so I just installed Flask here and you can already see that it came with a bunch of third-party dependencies, and now these should actually show up if I run
pip freeze again, as secondary or transitive dependencies.
Alright, so there we go, when I run
pip freeze again, with Flask installed, it’s also listing the other dependencies or the secondary dependencies that Flask brought with it. Because really all I did here initially was run
pip install flask and then Flask itself brought in all of these other secondary dependencies.
Capturing these secondary dependencies is super important to make an environment reproducible. As you can see here,
pip freeze does exactly that, so if I regenerate my requirements file, you can see that this captures all of the dependencies, so with this requirements file, we’re in a pretty good shape to completely reproduce this exact environment on another machine including the exact version specifiers and including all of the dependencies and secondary dependencies that our application might need.
Here is a quick recap on capturing dependencies using the
pip freeze command, basically you’ll run the
pip freeze command and then take its output to create your
requirements.txt file, and then you would include the requirements file with your project so that another developer can recreate the exact same environment that you were using at the time when you created the
pip freeze file.
04:47 Please note that the requirements file only specifies third-party packages and their version numbers, so this is not going to specify exactly which version of the Python interpreter to use for example.
pip freeze is the most important ingredient for achieving repeatability, so with
pip freeze you can capture all dependencies of your programs including secondary dependencies and their exact version numbers.
05:14 So it’s really important to capture the secondary dependencies also because if you only specify the first level or top level dependencies, what could happen is there might be a silent update for a secondary dependency and this could then cause trouble down the line when someone tries to install your program at a later point in time.
Become a Member to join the conversation.