How to Create Requirements.txt Python?
A requirement.txt python file is a type of file that usually stores information about all the libraries, modules, and packages specific to the project used while developing a particular project. This article will guide us in creating a requirements.txt file in Python and installing dependencies from the requirements.txt file. We will be looking into various ways to create requirements.txt in Python.
Need for Requirements.txt File
While working on Python Projects, we have probably noticed a file called requirements.txt. This requirements.txt file is used for specifying what python packages are required to run the project. It stores the information of what packages with specified versions are needed for running the project. With a requirement.txt file we can get started with the application and install all the required dependencies in a single command. It plays a crucial role as we start developing our Python application. We would be using specific versions of packages. Any change in the version might break your code. Globally we installed many packages, but in a particular project, we might need fewer.
Problems with the Traditional Requirements.txt Files
Traditionally, we create a requirements.txt file by creating a virtual environment. After activating a virtual environment, we have to run the command as follows:
It works fine, but the problem with this approach is that it includes all the packages that are installed via pip install <package_name> and the sub-dependency packages. Going with this approach adds up many sub-packages that are optional to install manually and come with main packages installation. Listing these all make the file long.
Let’s consider the following scenario: We are working on Django based application, and the only package we have installed is Fastapi through the command :
However, when we tried to put all the installed packages into the requirements.txt file apart from Django, additional packages were installed internally by pip for Fastapi. We can use the grep command to filter for only required packages to avoid this. The command to pass the standard output from pip freeze to grep is as follows :
The | command is called a pipe. It is used to pipe, or transfer, the standard output from the command on its left into the standard input of the command on its right.
The shell command grep is used to search files for lines that match a pattern and returns the results. Various options can be specified along with the grep command to specify the search.
What is a Virtual Environment?
In basic, a virtual environment is an isolated environment for python projects. It allows you to create an isolated environment for each python project. This makes it easier for us to manage packages and dependencies throughout projects, especially where they share the same dependencies. Various ways exist to create a virtual environment and make a requirements.txt file. Some of them are as follows :
- virtualenv - Virtualenv is a library that allows us to execute a virtual environment.
- pipenv - Pipenv is a dependency manager for Python projects.
- Pipreqs - Pipreqs is another alternative that works without creating a virtual environment in first place.
Once the virtual environment is created for our project, let’s see how to use different packages to generate a requirements.txt file. Let’s first explore how to use the Virtualenv packages.
Working with Virtualenv
Virtualenv is a library that allows us to execute a virtual environment. To install and work with it, you can install it through the following pip command:
After installation and setting up the environment, we need to activate the environment using the source :
source is a shell built-in command used to read and execute` the file content (generally set of commands), passed as an argument in the current shell script.
Once the virtual environment is activated, the name of your virtual environment will appear on the left side of the terminal. This will let you know that the virtual environment is currently active.
Now you can install dependencies related to the project in this virtual environment. For example, if you use Fastapi 0.77.1 for a project, you can install it like other packages.
How to Get the Requirements.txt File: Using Virtualenv
Creation of a requirements.txt through Virtualenv could be done through a pip freeze command following Redirecting command to the requirements.txt file.
First, pip freeze would generate a list of packages that are required or installed through pip commands, followed by the > symbol, which takes all the generated text or names as output and gives to the requirements.txt file as input if the requirements.txt file is absent before the same command would create the command execution, a new file.
The > symbol redirects output by taking the output from the command on the left and passing as input to the file on the right.
How to Get the Requirements.txt File: Using Pipenv
Pipenv is a Python** packaging tool** that solves common problems associated with the typical workflow using pip, virtualenv, and the good old requirements.txt. To get started with installation, we can follow the below-mentioned commands :
Install
Install Your Packages for the Project
Once we are done installing pipenv, we can effectively forget about pip since Pipenv essentially acts as a replacement in place of pip. It also introduces two new files, the Pipfile (meant to replace requirements.txt) and the Pipfile.lock (which enables deterministic builds). This Pipfile would be containing all the packages and sub-packages which are being installed by pip command or externally by other packages. Pipenv uses pip and virtualenv under the hood but simplifies their usage with a single command line interface.
Activate Virtual Env
Run a Script in the Virtual Env
Pipenv is a dependency manager for Python projects. It is similar to those tools if you’re familiar with Node.js’ npm or Ruby’s bundler. Pipenv is recommended as it simplifies dependency management for everyday use cases. Pipenv manages dependencies on a per-project basis.
By default, it generates a Pipfile, which contains all the packages that are installed by pip command or being installed by external packages with their mentioned versions. Whereas if we want, we can also generate a requirements.txt file following the below-mentioned command:
How to Get the Requirements.txt File: Without VirtualEnv Using Pipreqs
Pipreqs is another alternative that doesn’t require you to create a virtual environment first. This method automatically generates Python dependencies for Python project management. It generates requirements.txt file based on the modules & packages you import into your project. It helps in not including extra installed libraries not being used by the Python project. Let us see how to work with it.
Installation
Once the pipreqs are installed, we can directly generate a requirements.txt file without creating a Virtual Environment. We have to point the path to our project folder or directory.
Pipreqs uses imports of projects to generate a requirements.txt file. So, it is essential to note that pipreqs will not include the external plugins required by the projects. These plugins might be used internally by the packages. You must manually add those in a requirement.txt for such cases.
Stand out from the crowd with a solid Python skillset. Enroll in our Free Python course and gain a competitive edge in your academic and professional pursuits.
Get Certified in Python
Elevate your coding career with our Python Certification Test – your gateway to proving your expertise in Python and boost your resume!
- Practical and use case based questions
- Detailed Analysis of your performance
- Personalized video recommendations tailored to address any gaps in your knowledge.
Get your Python certification now
Conclusion
-
In Python, a requirements.txt file is a type of file that usually stores information about all the libraries, modules, and packages in itself that are used while developing a particular project.
-
The most common way to create a requirements.txt file is to run pip freeze > requirements.txt when all packages are installed.
-
Pipenv is a Python packaging tool that solves common problems associated with the typical workflow using pip, virtualenv, and the good old requirements.txt.
-
Pipenv acts as a replacement in place of pip. It also introduces two new files, the Pipfile (meant to replace requirements.txt) and the Pipfile.lock (which enables deterministic builds).
-
Pipreqs is the other simple alternative that doesn’t require first creating a virtual environment. This is quite useful and easy to operate. It generates a requirement.txt file based on the import statements of the project.
-
Pipreqs uses imports of projects to generate a requirements.txt file. So, it is essential to note that pipreqs will not include the external plugins required for specific projects.