Creating a requirements.txt file in Django

django create requirements txt
10 November 2024

If you are also a supporter of Python and Django, you probably often encounter projects that need to be shared or set up on another server. One of the most important things that should be prepared is a list of dependencies of the project that should be stored in a file named requirements.txt. This file will help other developers or even the server maintainers know which versions of packages are needed to run the project correctly.

The requirements.txt file is essentially a list of the dependencies of the project. Its contents are listed in a flat manner, including the package name along with its version specification. Django, both as a direct dependency and as a support for several packages that always need to be updated or modified, usually requires multiple packages that every time must be installed; it's merely sufficient to read them from this file!

Fortunately, creating this file is very simple! If you have Python and pip installed, you only need your own tool to use to easily create and update the dependency file. This task can be done via pip freeze, which lists all the installed packages and redirects them into a file. How should we look at this line by line?

To create a requirements.txt file, first ensure that all the required packages are installed correctly. Then, you can run the following command:


pip freeze > requirements.txt

Now, let's look at this command line by line:

  • pip freeze: This command lists all globally installed packages and shows their versions.
  • > requirements.txt: This operator takes the previous command's output (which lists the packages and their versions) and saves it in the requirements.txt file.

In this way, you now have a file that contains all the necessary dependencies to run the project, and anyone else who looks at this project will be able to install it easily without any confusion or difficulties with versioning, installation, and execution!

FAQ

?

Why should I create a requirements.txt file?

?

Do I always have to use pip freeze?

?

How can I add a new dependency to the file?