Managing Python Dependencies Transcripts
Chapter: Setting Up Reproducible Environments & Application Deploys
Lecture: Restoring Captured Dependencies

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Now that you know how to capture dependencies using the pip freeze command,
0:05 let's talk about how you can take a requirements file
0:09 and restore the dependencies of a program.
0:12 Early on I said that requirements files
0:15 are really just a bunch of pip install commands inside a text file,
0:19 and this is going to explain how we can take a requirements file
0:23 and use it to reinstall all of the dependencies that are listed in it.
0:27 To do that, you need to call the pip install command
0:30 and use it with the -r command line flag
0:34 and then pass the name of the requirements file to it.
0:37 Let's take a look at how that works in practice.
0:40 So I am back in the previous directory that I used in the pip freeze example,
0:44 but I've recreated the virtual environment from scratch so that it's now empty again.
0:48 Running the pip list command shows that that's the case.
0:54 But this directory still includes the requirements.txt file
1:01 that I created the last time around, let's take a quick look at it.
1:05 So the requirements.txt file here lists all of the third party dependencies
1:09 that I used in the previous example,
1:12 now I am going to use the requirements.txt file
1:15 to reinstall all of these dependencies in their exact same versions listed here.
1:19 The command you need for that is pip install -r
1:24 and then you pass the name of the requirements file,
1:28 I am going to go ahead and run this now.
1:33 As you can see here, this went over
1:36 all of the requirements listed in the requirements.txt file
1:39 and reinstall them in the exact same versions I used before.
1:43 So now when I do a pip list you can see that this recreated
1:46 the environment I was using previously.
1:49 So this set of third party dependencies is an exact replica
1:53 of the ones that I used in the previous example
1:56 and I was able to restore them
1:58 from the requirements file using the pip install command.
2:01 You just saw how you can restore Python dependencies,
2:05 using a requirements file and the pip install command,
2:08 let's do a quick review of the full workflow for capturing
2:11 and restoring Python dependencies using requirements files.
2:15 Really what this comes down to is a three step process.
2:18 The first step, happens during ongoing development
2:22 where you install necessary dependencies as you're working on your program.
2:26 For example, you might decide to install the Requests package
2:32 because you need to support HTTP downloads in your program.
2:35 So you would just go ahead and do a pip install requests to install that package.
2:39 And when you're ready to deploy your program
2:42 or even if you just need to take a snapshot to send to git
2:45 or another version control system in order to share it with other developers,
2:49 then you move on to step 2
2:51 and you capture the dependencies inside a requirements file.
2:55 For that you would use the pip freeze command
2:57 and take its output and store it inside a requirements.txt file.
3:01 Now every time you want to deploy your program to another machine
3:05 or want to set up another development environment
3:07 on a different developers machine,
3:09 that is when step 3 comes into place, this is where we restore
3:13 the dependencies from the requirements file you created earlier,
3:16 for that, you would run the pip install command with the -r requirements.txt flag.
3:23 This installs or updates all of the dependencies
3:26 that were stored in the requirement's file in step 2.
3:29 Because the output of the pip freeze command also includes
3:33 specific version numbers for those packages,
3:35 this will recreate the exact same set of dependencies
3:38 including secondary dependencies.