Declaring Requirements
00:00 Imagine you’re working on a Python project with a friend. You just created the virtual environment on your computer, activated it, and you installed a bunch of packages.
00:10 Now your friend wants to have the same development environment as you have, with exactly the same packages that you installed. Now you have two choices.
00:21
The first choice is that you tell your friend all the package names you have. They could install all packages with the pip install
command.
00:29
A convenient thing about pip install
is that you can chain package names. So you use the pip install
command, and then you have like the package-name-1, the package-name-2, the package-name-3, and so on.
00:40 But there is another way that is even more convenient.
00:45 Another way to make sure that your friend is using the same packages as you do is by declaring requirements. You do this by first freezing the list of your packages in a TXT file.
00:58
This text file is commonly named requirements.txt
. Then you can share this file with your friend, and your friend can use this file to install all requirements with the pip install
command and the -r
option.
01:13 Let’s have a look at this in action.
01:17
I’m currently in the project directory with an activated virtual environment. First, let’s have a look what packages are installed by running python3 -m
pip list
. So currently, there are pip
and setuptools
.
01:33
Let’s install the requests
package again, and to make it more interesting, let’s also install the rich
library. Rich enables you to create a colorful Python application for the terminal.
01:45
I’ll show you the capabilities in a moment. Let’s install them first. To install both packages at the same time, You type python3 -m pip install
requests
and then, with a space in between, rich
and then press Enter.
02:06
Okay, now I can show you the cool things that Rich can do. Type python3 -m rich
.
02:15
Isn’t this amazing? Depending on your terminal capabilities, the output might look a bit different. So we won’t go into detail about the packages you install with pip
in this course, but I’ll mention some Real Python resources for both Requests and Rich at the end of this course.
02:32
But I wanted to show you how to run Rich because we’ll try it out without an activated virtual environment in a moment. Okay, let’s move on. When you type clear
and press Enter, you clear your terminal window.
02:48
As you know, when you installed requests
and rich
, you probably didn’t just install the packages itself, but also their dependencies. To see them, you run pip list
, but there’s another command that you can use.
03:02
If you type python3 -m pip
freeze
, then the output looks very similar to the pip list
output. However, the syntax is a bit different.
03:16
You can see that there are equal signs (==
) there. This syntax lets pip
know exactly what versions to use when installing requirements, and the version of a package is actually a quite important detail. When you work with others on a project, you want to make sure that you are all working with the same package version, for example. Otherwise, an update might introduce a change that makes your code break, and you need to adjust your Python code accordingly.
03:45 So that’s why it’s even a good idea to freeze your requirements for yourself as well, even when you’re not working with others. You always want to develop in an environment where you know exactly which versions of your packages you’re working with. Okay, enough rambling.
04:02
I think you got the message. This output of pip freeze
with the version numbers is important. To save this output in a file, you can use the caret symbol (>
). With the caret, you can redirect the output of a command into a file.
04:16
So when you run python3 -m pip freeze
and then a caret—that’s the greater-than sign—requirements.txt
, then you send a package list of pip freeze
into a file named requirements.txt
.
04:33
If the file doesn’t exist, you create it. If the file is present already, then you overwrite the file with the output of pip freeze
.
04:43
So you can run this command every time when you install new packages for your project, and therefore you change your requirements for your project, and you can run pip
freeze
again and save the output in the requirements file, so that way you have your requirements frozen for the current state of your project.
05:01
You can look at the content of a file with the cat
command. So if you type cat requirements.txt
, then you see the content of the requirements file. Look, this is exactly the output of pip freeze
, so it worked. Perfect. With clear
, I can clear a terminal again.
05:21
Let’s deactivate a virtual environment and move into the parent directory with cd ..
(dot dot).
Become a Member to join the conversation.