Requirements Files
Requirements Files
When a project uses installed libraries, other people need those same libraries too.
Instead of listing packages manually, Python projects often use a requirements file.
What Is requirements.txt?
requirements.txt is a text file that lists the libraries your project needs.
Example:
requests==2.31.0
flask==3.0.0
pandas==2.2.0
Each line is one package.
Why It Matters
It helps you:
- share project dependencies
- recreate environments
- install packages quickly
- keep teams consistent
Create a Requirements File
After installing packages, run:
pip freeze > requirements.txt
This saves installed packages into:
requirements.txt
Install from requirements.txt
Another user can run:
pip install -r requirements.txt
This installs all listed packages.
Example Project
my_project/
main.py
requirements.txt
Update the File
If you install new packages later, run again:
pip freeze > requirements.txt
Best Practice
Use a virtual environment first.
Then create requirements.txt.
This keeps the file cleaner.
Common Beginner Errors
Running Outside venv
You may save too many global packages.
Wrong File Name
Use:
requirements.txt
Forgetting Version Numbers
Versions help keep results consistent.
Code Along
Create a virtual environment.
Install one package.
Generate requirements.txt.
Mini Challenge
Tasks:
- Install
requests - Create
requirements.txt - Delete environment
- Create a new environment
- Reinstall from the file
Expected result:
Same packages installed again.
Real World Use Case
Teams use requirements files in apps, APIs, data projects, automation tools, and deployments.
Quiz
- What is
requirements.txt? - What does
pip freezedo? - How do you install from the file?
- Why are versions useful?
Assignment
Create a small project with one library and generate a requirements.txt file.
Summary
You learned how requirements.txt helps share and recreate Python project dependencies.