Restoring Captured Dependencies
Now that you know how to capture dependencies using the
pip freeze command, let’s talk about how you can take a requirements file and restore the dependencies of a program.
Early on I said that requirements files are really just a bunch of
pip install commands inside a text file, and this is going to explain how we can take a requirements file and use it to reinstall all of the dependencies that are listed in it.
To do that, you need to call the
pip install command and use it with the
-r command line flag and then pass the name of the requirements file to it.
Let’s take a look at how that works in practice. So I am back in the previous directory that I used in the
pip freeze example, but I’ve recreated the virtual environment from scratch so that it’s now empty again.
pip list command shows that that’s the case. But this directory still includes the
requirements.txt file that I created the last time around, let’s take a quick look at it.
requirements.txt file here lists all of the third-party dependencies that I used in the previous example, now I am going to use the
requirements.txt file to reinstall all of these dependencies in their exact same versions listed here.
The command you need for that is
pip install -r and then you pass the name of the requirements file, I am going to go ahead and run this now.
As you can see here, this went over all of the requirements listed in the
requirements.txt file and reinstall them in the exact same versions I used before.
So now when I do a
pip list you can see that this recreated the environment I was using previously. So this set of third-party dependencies is an exact replica of the ones that I used in the previous example and I was able to restore them from the requirements file using the
pip install command.
You just saw how you can restore Python dependencies, using a requirements file and the
pip install command, let’s do a quick review of the full workflow for capturing and restoring Python dependencies using requirements files.
02:15 Really what this comes down to is a three step process. The first step, happens during ongoing development where you install necessary dependencies as you’re working on your program.
For example, you might decide to install the Requests package because you need to support HTTP downloads in your program. So you would just go ahead and do a
pip install requests to install that package.
02:39 And when you’re ready to deploy your program or even if you just need to take a snapshot to send to Git or another version control system in order to share it with other developers, then you move on to step 2 and you capture the dependencies inside a requirements file.
For that you would use the
pip freeze command and take its output and store it inside a
requirements.txt file. Now every time you want to deploy your program to another machine or want to set up another development environment on a different developers machine, that is when step 3 comes into place, this is where we restore the dependencies from the requirements file you created earlier, for that, you would run the
pip install command with the
-r requirements.txt flag.
This installs or updates all of the dependencies that were stored in the requirement’s file in step 2. Because the output of the
pip freeze command also includes specific version numbers for those packages, this will recreate the exact same set of dependencies, including secondary dependencies.
Hi Dan! what happens if I restore my virtual environment, but it already contains some other packages? Will they be erased? Or is it a bad practice and I should restore it on an “empty” venv as you have made in the video? Thanks!
@László Szatmáry the packages in the existing venv won’t be erased, unless you use the
--clear option when creating your new venv.
And like you said, it’s best practice to treat your venv as disposable, delete the old one, and create a fresh new one before installing dependencies.
Become a Member to join the conversation.
RicardoSantana on June 4, 2020
I did not learn about the need for virtual environments and more so, the need to be diligent and create a requirements.txt file to enable replication of whatever project. Every minute I have spent in this training as been time well spent. Big Thank You Dan.