What Are Requirements Files?

written by Chris Goodwin on 03/04/2021

We all use various python libraries for this or that. Django is a framework that we install and use by running pip install django which is pretty simple!

Every project has requirements

If you develop in python, odds are you will find yourself using at least one or two (or maybe ten or twenty) additional libraries. When it comes time to deploy your application/project, it sure would stink to have to type in pip install some-library for all the additional libraries you used, right?

That is what requirements.txt is for!

For best results - you want to combine requirements.txt with using virtual environments. You create a virtual environment, activate it, and then install your libraries from your requirements file. That ensures that the specific environment you created has all the appropriate libraries you need when using it. It really is just that simple!

You can include all the additional libraries you need in your requirements.txt file like this:

Django==3.1.7
requests==2.25.1
stripe==2.56.0
#...etc...

This is called 'pinning' your requirements files. You specify the specific version you are using in your requirements.txt and you can install all of the libraries listed there by one command:

pip install -r requirements.txt

It is that simple!

The workflow in entirety would probably look something like this:

virtualenv venv # Create the virtual environment

source venv/bin/activate # Activate the virtual environment

pip install -r requirements.txt # Install additional libraries

...and you now have an environment where all the required libraries are installed for your project/application! That makes it much easier to manage/maintain the additional libaries your project/application is using that trying to remember which libraries to install.

What about libraries needed just for development?

Yes, some libraries you only need for development and they are not needed in the production environment.

In this case? You can just create two files:

  • requirements.txt # This contains just the libraries needed for production
  • requirements-dev.txt # This contains additional libraries needed only for development

In our requirements-dev.txt file, we simply tell it to include all the libraries in requirements.txt as well as all the other libraries we need that are specific to our development process.

The requirements-dev.txt would look like this:

# Include the libraries needed for production
-r requirements.txt
# These libraries are only needed for development
django-debug-toolbar==3.2
factory-boy==3.2
freezegun==1.1.0
# ...etc...

The first line ensures it will install all the 'core requirements' that the project needs, but then it also installs the additional libraries needed for development.

This can be ran with pip install -r requirements-dev.txt

This gives you options

Now that you have a requirements.txt and a requirements-dev.txt you have options!

For local development? You can run pip install requirements-dev.txt to be up and running with all your required libraries!

For production? You can run pip install requirements.txt to be up and running with all the production appropriate libraries and nothing extra.

Some people struggle with requirements files - but hopefully this helps to demystify what they are used for and how they can help you install just the right libraries for the environment you are using!

Cheers!

Did you find this article helpful?

Feel free to help me keep this blog going (with coffee)!