I started with installing packages individually, but in most cases you'll actually want to install packages in bulk using a requirements.txt file.
Using everything we learned above, we can create a single file that lists all of the packages we want to use in our project.
So I'll create a file (usually located in the root of the project directory) called requirements.txt, and add the following:
AwesomePackage==2.0
SomeOtherPackage==1.0
XYZPackage==3.2.5
Now, after saving the file and exiting, we can install all of the packages together:
pip install -r requirements.txt
Why use requirements.txt?
There are a few obvious reasons to use a requirements.txt file.
- It provides a list of your projects dependencies.
- Other developers can easily install all required packages on their dev environments.
- It simplifies the installation process.
Pip freeze
To confirm the packages were installed you can use pip freeze
.
pip freeze
Note: This will show all installed packages. Sometimes, the packages we list in requirements.txt will depend on other packages that will come along for the ride.
To look for specific packages, use grep
:
pip freeze | grep "AwesomePackage"
A note on pinning versions
Your requirements.txt file doesn't have to include version numbers. It can look like this:
AwesomePackage
SomeOtherPackage
XYZPackage
This is a little bit dangerous. As soon as new versions of any of these packages are available, future builds will automatically include them. This could break your code. To remedy this situation, pin each package to a specific version and only upgrade packages intentionally.