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, let's talk about how you can take a requirements file
0:10
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,
0:20
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.
0:28
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.
0:38
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,
0:45
but I've recreated the virtual environment from scratch so that it's now empty again. Running the pip list command shows that that's the case.
0:55
But this directory still includes the requirements.txt file that I created the last time around, let's take a quick look at it.
1:06
So the requirements.txt file here lists all of the third party dependencies that I used in the previous example,
1:13
now I am going to use the requirements.txt file to reinstall all of these dependencies in their exact same versions listed here.
1:20
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.
1:34
As you can see here, this went over all of the requirements listed in the requirements.txt file
1:40
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
1:47
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
1:57
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,
2:06
using a requirements file and the pip install command, let's do a quick review of the full workflow for capturing
2:12
and restoring Python dependencies using requirements files. Really what this comes down to is a three step process.
2:19
The first step, happens during ongoing development where you install necessary dependencies as you're working on your program.
2:27
For example, you might decide to install the Requests package because you need to support HTTP downloads in your program.
2:36
So you would just go ahead and do a pip install requests to install that package. And when you're ready to deploy your program
2:43
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,
2:50
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
2:58
and take its output and store it inside a requirements.txt file. Now every time you want to deploy your program to another machine
3:06
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
3:14
the dependencies from the requirements file you created earlier, for that, you would run the pip install command with the -r requirements.txt flag.
3:24
This installs or updates all of the dependencies that were stored in the requirement's file in step 2.
3:30
Because the output of the pip freeze command also includes specific version numbers for those packages,
3:36
this will recreate the exact same set of dependencies including secondary dependencies.