How to Create requirements.txt in Python

Vaibhav Vaibhav Feb 02, 2024
  1. Create the requirements.txt Using pip Package Installer
  2. Install Dependencies From requirements.txt Using pip Package Installer
How to Create requirements.txt in Python

When developing Python applications, we have to use a bunch of modules for a variety of features. The number of modules being used by an application can be a lot. Generally, when developing such gigantic applications and even the smaller ones, creating a virtual environment specific to the project is recommended because it lets us install whatever we wish to and of whichever version without brimming the global package space.

If our friends, family, or colleagues wish to use the developer on their system, they would also require the code and dependencies installed on their end. Since the dependencies are installed in a virtual environment, sharing the whole virtual environment does not make sense because the folder size will be massive, and they can face errors due to integrity issues.

In such cases, developers add a requirements.txt file to a project containing a list of all the dependencies installed in the virtual environment and the details of the version being used. This way, the borrower or the end-user only has to create a virtual environment and install the dependencies to use the application.

This article will guide us on creating the requirements.txt file and installing dependencies from the requirements.txt file.

Create the requirements.txt Using pip Package Installer

To generate a requirements.txt file, we can use the pip package installer or package management system from the command line. Refer to the following commands for the same.

pip freeze > requirements.txt
pip3 freeze > requirements.txt

In case, instead of pip, if you are using the conda package manager, you can use the following command to generate a requirements.txt file.

conda list -e > requirements.txt

Install Dependencies From requirements.txt Using pip Package Installer

Once we have generated a requirements.txt file, we can use this file to install all the dependencies mentioned inside it. Refer to the following command for the same.

pip install -r requirements.txt

Generally, it is recommended to create a virtual environment before starting any new project and installing any dependency. This makes sure that you do not clutter your global package space with random and uncommon packages. The workflow for the same would be as follows.

  1. Create a virtual environment.
  2. Activate the virtual environment.
  3. Install the dependencies.

Refer to the following commands for the same.

virtualenv environment # Create a virtual environment
environment\Scripts\activate # Activate the virtual environment
pip install -r requirements.txt # Install the dependencies
Vaibhav Vaibhav avatar Vaibhav Vaibhav avatar

Vaibhav is an artificial intelligence and cloud computing stan. He likes to build end-to-end full-stack web and mobile applications. Besides computer science and technology, he loves playing cricket and badminton, going on bike rides, and doodling.

Related Article - Python Installation