How to Generate a Requirements File in Python

Kris Pickering · March 24, 2018

You might of heard of a requirements folder in your travels to learn Django or Python. Hopefully the short explanation below helps you understand how and why it is used.

The reason we have a requirements file is so that we can list all the Python packages that our project is currently using but why do we do this? We can use this file to automatically install our packages to get our project up on running on new machines quickly or when sharing our project to fellow developers.

Creating our requirements.txt file.

First off make sure you have activated your virtual environment and have Pip installed. Once you have checked this you just need to run the following command.

(our_project) $ pip freeze > requirements.txt

That was it, pretty simple. Next we’ll show you how to tell Pip to read our requirements and download all our packages.

Installing packages using our requirements.txt file.

Again, this code should be run when sitting inside your virtual environment.

(our_project) $ cd /path/to/requirements/file (our_project) $ pip install -r requirements.txt

And that’s all there is to it. With just a few commands we can make managing our packages easier when moving our code into new environments.

Twitter, Facebook