How to Use requirements.txt to Install All Dependencies in a Python Project

Managing dependency in Python is very important to ensure that your project works in a stable and proper manner. The most common method to handle and install the dependency is by the use of a requirements.txt file. It’s actually a list of all the libraries and packages mentioned with their version, which makes the project setup a lot easier.

The aim of this blog is to guide you through the process of creating a requirement file and if it’s already available, how you can use the same.

Table of Contents

Introduction to requirements.txt

A requirements.txt file is a plain text file used by Python projects to define all the dependencies that are required for the project. The file lists the libraries with their respective versions, ensuring that anyone working on the project has the same setup.

What is the use of requirements.txt?

  • Reproducibility: It enables you to reproduce the same environment, which is critical for collaboration, deployment, and scalability.
  • Version Control: You can declare specific versions of dependencies to prevent problems due to changes in future library versions.
  • Simplifies Setup: It is simple to set up the project on new machines or virtual environments.

Creating a requirements.txt File

1. Manually Adding Dependencies

To create a requirement.txt file yourself, you must specify the libraries and their versions you wish to use in your project. Here’s how you can do it:

matplotlib==1.21.0
django==1.3.0
seaborn>=2.25.0
Flask==2.0.1

Just ensure that when you save the file, it should be in.txt format. This file will be utilized to install the libraries you specified.

Creating a requirements.txt File Image

2. Generating the Requirement File Automatically

If you have already installed all the dependencies into your environment, then you can create a requirements.txt file by using the pip freeze command. This will take out all your packages with their versions that have been installed, and these can be redirected to a requirements.txt file:

pip freeze > requirements.txt

This will give you all the libraries or the packages that are installed in the environment you are currently working in.

Installing Dependencies from requirements.txt

1. Using pip to Install Dependencies

If you already have a requirement file with you, then the installation of the same is quite easy. You need to use -r parameter with pip command. Below is the code, that will help you perform the same:

pip install -r requirements.txt

This operation will automatically install all the libraries/packages that were listed in your requirement.txt file.

2. Installing Specific Versions of Packages

You can also install the specific version using your requirements.txt file. This can be used when you are working on a project that has some other specific requirements other than the normal requirements.  Here is how the same can be done:

  • In order to install a specific version:
    • requests==2.25.0
  • If you want to install a version that is greater than or equal to a particular version:
    • numpy>=1.21.0
  • In case you want to install a version within a range:
    • pandas>=1.2.0,<1.3.0

Best Practices to Manage the Dependencies

  • Usage of Different Virtual Environments: You can use different virtual environments for different projects as it prevents conflicts between dependencies of different projects. You can use venv or virtualenv for this purpose.
  • Keep Dependencies Up-to-date: You should update the packages in your requirements.txt to include your bug fixes and new features. Use pip list –outdated for checking the outdated packages.
  • Avoid Unnecessary Packages: Keep your requirements.txt file minimal. Only include packages that are necessary for your project’s functionality.
  • Pin Versions: It’s recommended to pin the version of each dependency (e.g., numpy==1.21.0) to avoid any issues with new versions of libraries.

Conclusion

The requirements.txt file is an important tool for managing dependencies in Python projects. It facilitates you to make sure that all the required libraries are loaded correctly and also avoids the issues that could be caused by clashing package versions. If you want to learn more about these techniques, then you should check out our Python Training and Certification.

About the Author

Senior Consultant Analytics & Data Science

Sahil Mattoo, a Senior Software Engineer at Eli Lilly and Company, is an accomplished professional with 14 years of experience in languages such as Java, Python, and JavaScript. Sahil has a strong foundation in system architecture, database management, and API integration. 

Full Stack Developer Course Banner