In this lesson, you will learn how to create a requirements.txt file so that your project's dependencies can be easily shared and recreated on any machine.
Why Do We Need Requirements Files?
When you work on a project, you often install various packages using pip. However, if you move your project to a new computer or share it with a friend, they won't know which packages or versions you used. If you try to run your code without installing the exact same dependencies, your program might crash.
A requirements.txt file acts like a "grocery list" for your project. It records exactly which packages are installed in your virtual environment so that anyone else can install them all at once.
How to Create Your Requirements File
Since you are already using a virtual environment, you can generate this list automatically. Open your terminal, ensure your virtual environment is activated, and run this command:
pip freeze > requirements.txt
The pip freeze command lists all the packages installed in your current environment, and the > symbol tells your terminal to save that list into a file named requirements.txt. If you open this file, you will see something like this:
requests==2.28.1
beautifulsoup4==4.11.1
How to Use the File
When you want to set up your project on a different machine, you simply create a new virtual environment, activate it, and run:
pip install -r requirements.txt
The -r flag tells pip to read the list from the file and install every package listed inside it. By keeping this file in your project folder, you ensure that your environment stays consistent, whether you are coding on your laptop or running your project on a server. It makes your work professional, organized, and—most importantly—reproducible.
Key Takeaway: A requirements.txt file allows you to capture your project's environment snapshot and share it so others can recreate your exact setup with a single command.