Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Restoring Captured Dependencies

Give Feedback

00:00 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.

00:12 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.

00:27 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.

00:37 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.

00:48 Running the 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.

01:05 So the 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.

01:19 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.

01:33 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.

01:43 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.

02:01 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.

02:26 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.

02:55 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.

03:23 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.

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.

Dan Bader RP Team on July 11, 2020

Thank you!

Become a Member to join the conversation.