Python - How to install all required packages using pip

If you are like me and have never had more than two or three packages as dependencies in a python project you have likely never needed an "install all" command. You can install all packages by creating and adding them to a "requirements.txt" file. The "requirements.txt" file specifies all the packages needed to run the project, for example if Flask version 2.1.2 is required it can be specified in the following way:

Flask==2.1.2

In order to install all the packages within the "requirements.txt" file you can run the following pip command:

pip install -r requirements.txt

If you want a hint to what packages you need to put into the requirements.txt file you can use the freeze command:

pip freeze

This gives you the packages which you have installed using pip.


Virtual environment

Before you do this you may want to use or set up your virtual environment to avoid conflicts when installing packages globally. You create a new virtual environment using the following:

python -m venv venv

and activate it using either the powershell script (windows):

venv\Scripts\Activate.ps1

or bash (linux):

source tutorial-env/bin/activate

After this you can run the pip install commands to only install the packages in your virtual environment, this avoids pollution by not installing packages globally.

That is it

I hope you enjoyed this post, I normally do not do Python posts, so let me know if I missed anything!