Subscribe to Our Newsletter

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

How to Create and Install Requirements.txt in Python

When working on a project with multiple dependencies, manually installing each package can be tedious.

Oyinebiladou Omemu profile image
by Oyinebiladou Omemu
How to Create and Install Requirements.txt in Python
Photo by Danial Igdery / Unsplash

Python is one of the most popular programming languages, but it doesn't come bundled with every package you might need for a project. For example, if you're building a web API with FastAPI, you’ll need libraries like Fastapi and uvicorn, which aren't included by default.

When working on a project with multiple dependencies, manually installing each package can be tedious. This is where the requirements.txt file comes in. This simple text file lists all the necessary packages so anyone working on the project can install them in one go. Let’s dive into how to create and use this file.

What is a requirements.txt File?

The requirements.txt file is a list of dependencies required for a project. Instead of installing packages one by one, you can use this file to install everything at once. Here’s an example of what a requirements.txt file might look like.

This file ensures that all necessary libraries are available, preventing compatibility issues when working across different environments.

Key Terms

  • Dependencies: Software components that a program needs to function correctly.
  • Packages: Bundled collections of code used to extend Python's functionality.
  • Virtual Environment: A self-contained directory that includes a Python interpreter and installed packages specific to a project.
  • Pip: Python’s package manager used to install, upgrade, and remove packages.

Creating a requirements.txt File

1/ Set Up a Virtual Environment

Before generating a requirements.txt file, it’s best practice to create a virtual environment. This ensures that dependencies are isolated and won’t interfere with other projects.

How to create a virtual environment in Python
Use virtual environment to manage dependencies and ensure project stability in Python development.

2/ Install Required Packages

Inside your virtual environment, install the necessary packages:

pip install fastapi uvicorn pydantic

3/ Generate the requirements.txt File

Once the required packages are installed, create the requirements.txt file by running:

pip freeze > requirements.txt

This will generate a requirements.txt file listing all installed packages along with their versions.

Installing Dependencies from a requirements.txt file

Now that you have a requirements.txt file, you or anyone else working on the project can install all dependencies with a single command:

pip install -r requirements.txt

This command reads the requirements.txt file and installs the listed packages automatically. By ensuring our requirements.txt file is available, anyone can replicate our environment and run our application without any issues.

Specifying Package Versions

When defining packages in requirements.txt, you can specify version constraints to control how dependencies are installed:

  • == (Equal to): fastapi==0.115.11 (Only installs version 0.115.11)
  • >= (Greater than or equal to): pydantic>=2.10.6 (Installs the latest version of 2.10 or above)
  • ~= (Compatible with): uvicorn~=0.34.0 (Installs any version 0.34.x)
  • != (Not equal to): fastapi!=0.109.0 (Avoids a specific version)

Benefits of Using requirements.txt

  • Dependency Management: Keeps track of all necessary packages.
  • Easy Collaboration: Allows others to install dependencies quickly.
  • Consistency Across Environments: Prevents version mismatches and compatibility issues.
  • Reproducibility: Ensures projects can be set up and run on different systems with minimal effort.

Conclusion

Using a requirements.txt file is an essential best practice for managing dependencies in Python projects, especially when working with teams. It simplifies the installation process, ensures consistency across environments, and allows for easy collaboration among developers. By defining dependencies explicitly, you reduce the risk of version conflicts and make it easier to deploy your application in different settings, whether for local development or production.

Incorporating a requirements.txt file into your workflow ultimately leads to a more efficient and organized development process. Whether you're setting up a new project or maintaining an existing one, this simple text file acts as a blueprint for your environment, ensuring that anyone, whether teammates or future you, can quickly set up and run the project with minimal effort.

Oyinebiladou Omemu profile image
by Oyinebiladou Omemu

Subscribe to Techloy.com

Get the latest information about companies, products, careers, and funding in the technology industry across emerging markets globally.

Success! Now Check Your Email

To complete Subscribe, click the confirmation link in your inbox. If it doesn’t arrive within 3 minutes, check your spam folder.

Ok, Thanks

Read More