CodingNic

Modules, Packages, and Virtual Environments

Requirements Files

Modules, Packages, and Virtual Environments 28 min read

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:

text
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:

bash
pip freeze > requirements.txt

This saves installed packages into:

text
requirements.txt

Install from requirements.txt

Another user can run:

bash
pip install -r requirements.txt

This installs all listed packages.

Example Project

text
my_project/
    main.py
    requirements.txt

Update the File

If you install new packages later, run again:

bash
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:

text
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

  1. What is requirements.txt?
  2. What does pip freeze do?
  3. How do you install from the file?
  4. 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.