×
   ❮   
PYTHON FOR DJANGO DJANGO FOR BEGINNERS DJANGO SPECIFICS Roadmap
     ❯   

VIRTUAL ENVIRONMENT

Requirements.txt usage

Using requirements.txt in Django Projects

The requirements.txt file is a handy way to list the dependencies of your Django project. It allows others to recreate the same environment on their machines by installing the same packages with the same versions.

Creating a requirements.txt File

To generate a requirements.txt file containing all the dependencies in your virtual environment, run the following command:

pip freeze > requirements.txt

This command will output a list of installed packages and their versions into a file named requirements.txt.

Installing Dependencies from requirements.txt

If you or someone else wants to recreate the environment, they can install all the required packages using the requirements.txt file by running:

pip install -r requirements.txt

This will ensure the exact same packages and versions are installed in the new environment.

Best Practices for requirements.txt

  • Update the file regularly using pip freeze to keep it current.
  • Share the file with collaborators to ensure everyone is using the same environment.

Conclusion

Using a requirements.txt file ensures consistency across environments and makes collaboration smoother. Let’s explore another tool for environment management, pipenv, in the next subtopic.