This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 3: Pip

The requirements file

face Josiah Wang Ivan Procaccini

Say you have installed some packages in your virtual environment, and have developed the best AI application ever. You have put your source code on a remote git repo. Now you need to work with some collaborators to improve it, and later share it with the rest of the world!

How do you make sure that everybody else can easily and consistently run your application, using the exact packages and dependencies that you used? It will be a hassle for people to install your packages one by one (and to make sure they are the correct versions)! It might also be hard for you to remember what exactly you have installed if you have to run this on another machine yourself!

Easy. You save a snapshot of a list of your currently installed packages by freezing it. Future users can then just take this frozen list and get pip to install directly from this list!

To get your snapshot, simply run pip freeze:

(my_env) user@MACHINE:~/ai_project$ pip freeze
cycler==0.11.0
joblib==1.1.0
kiwisolver==1.3.2
matplotlib==3.4.3
numpy==1.21.3
pandas==1.3.4
Pillow==8.4.0
pyparsing==3.0.4
python-dateutil==2.8.2
pytz==2021.3
scikit-learn==1.0.1
scipy==1.7.1
six==1.16.0
threadpoolctl==3.0.0

You can save this list by piping the output to a text file rather than to your terminal. People usually name the file requirements.txt.

(my_env) user@MACHINE:~/ai_project$ pip freeze > requirements.txt

You would then place this file in the root directory of your git repo and push it upstream. This is standard convention in most open source projects. For example, the PyTorch repo has a requirements.txt file in its root directory.

Thawing your requirements list

Now you, your collaborators and your fans can run your code on another machine by using pip to install the packages directly from requirements.txt (use the -r flag for this). Hurray!

(my_env) user@MACHINE:~/ai_project$ pip install -r requirements.txt

That’s a quick introduction to pip. It is likely all you really need to know. You can refer to the Python Packaging User Guide for more advanced tips.