Restoring Captured Dependencies
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.
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.
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.
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.
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.
Become a Member to join the conversation.