рд╣рд┐рдВрджреА рдХрд╣рд╛рдирд┐рдпрд╛рдБ | рдЗрдиреНрдлреЛрд╕рд╛рдЧрд░

How to Create and Update requirements.txt in Python (The Right Way)

Dec 27, 2025
(0)

Why do you need this file?

     Imagine you send your Python code to a friend or upload it to a server. They try to run it, but it crashes immediately with an error: ModuleNotFoundError.

    Why? Because your computer has libraries (like Django, Pandas, or NumPy) installed, but their computer does not.

   

   A requirements.txt file is like a shopping list for your project. It tells other computers exactly which packagesтАФand which specific versionsтАФthey need to    download to make your code work.

 

  Here is how to manage it easily.

  

  Step 1: Always Activate Your Virtual Environment

  Before you do anything with requirements.txt, you must be inside your virtual environment. If you don't do this, you might accidentally include every single   Python package installed on your entire laptop (even ones this project doesn't use!).

 

  Windows: venv\Scripts\activate

  Mac/Linux: source venv/bin/activate

 

 (You know it works when you see (venv) in your terminal).

 

  Step 2: Creating the File (The First Time)

  Once your environment is active and you have installed your packages (like pip install django), you need to save that list to a file.

  Run this simple command in your terminal:

 

 pip freeze > requirements.txt

 

 What just happened?

  • pip freeze: asks Python "What is currently installed in this environment?"
  • >: takes that answer and writes it into a file instead of the screen.
  • requirements.txt: the name of the file (you can name it anything, but this is the standard).

You will now see a new file in your folder containing lines like Django==4.2.7.

 

Step 3: Updating the File (When You Add New Packages)

   This is where most people get confused.

    If you install a new package, for example:

 pip install requests

 

Your requirements.txt file does NOT update automatically. If you check the file, "requests" won't be there yet.

To update it, you must run the freeze command again:

 

pip freeze > requirements.txt

This will overwrite the old file with the new, updated list of packages.

 

Step 4: How to Use the File (Installing)

   If you move your project to a new computer (or a friend wants to run it), you don't need to install packages one by one.

  Just run this "magic" command:

 

pip install -r requirements.txt

This tells pip: "Read this file and install everything listed inside it."

 

Summary Checklist

  1. Activate your virtual environment.
  2. Install your packages (pip install package_name).
  3. Freeze them to the file (pip freeze > requirements.txt).
  4. Repeat step 3 every time you install something new.

 

16 Views

You Might Also Like

How to Setup Docker Compose in Django: The Ultimate Beginner's Guide (2025) Python

How to Setup Docker Compose in Django: The Ultimate Beginner's Guide (2025)

Running a Django project on your local machine is easy. But moving it to a colleague's laptop or a production server? ThatтАЩs where the "It works on my machine" nightmares begin. Docker solves this by packaging your code, database, and dependencies into a single, portable unit. Docker Compose takes it a step further by letting you run your Django app and your database (like PostgreSQL) with a single command.In this guide, we will turn a standard Django project into a Dockerized super-app in 5 easy steps. PrerequisitesBefore we start, make sure you have:Docker Desktop installed (download from docker.com).A basic Django Project ready (or create one using django-admin startproject myproject).Step 1: Create a requirements.txtDocker needs to know which Python packages to install. If you haven't already, generate this file in your project's root folder. pip freeze > requirements.txtMake sure django and psycopg2-binary (for Postgres) are listed inside. Step 2: The DockerfileA Dockerfile is like a recipe card that tells Docker how to build your Python environment. Create a file named Dockerfile (no extension) in your project root and paste this: # 1. Use an official Python runtime as a parent image FROM python:3.10-slim # 2. Set environment variables ENV PYTHONDONTWRITEBYTECODE 1 ENV PYTHONUNBUFFERED 1 # 3. Set the working directory in the container WORKDIR /app # 4. Install system dependencies (optional, but good for Postgres) RUN apt-get update && apt-get install -y libpq-dev gcc # 5. Install Python dependencies COPY requirements.txt /app/ RUN pip install --upgrade pip RUN pip install -r requirements.txt # 6. Copy the project code into the container COPY . /app/  Step 3: The docker-compose.yml fileThis is the magic file. It orchestrates your Django app and your Database so they can talk to each other. Create a file named docker-compose.yml in your project root: version: '3.8' services: # Service 1: The Database db: image: postgres:15 volumes: - postgres_data:/var/lib/postgresql/data environment: - POSTGRES_DB=myproject_db - POSTGRES_USER=myuser - POSTGRES_PASSWORD=mypassword # Service 2: The Django App web: build: . command: python manage.py runserver 0.0.0.0:8000 volumes: - .:/app ports: - "8000:8000" environment: - DEBUG=1 - SECRET_KEY=foo - DJANGO_ALLOWED_HOSTS=localhost 127.0.0.1 [::1] depends_on: - db volumes: postgres_data:WhatтАЩs happening here?db: Pulls the official PostgreSQL image and sets up a default database user/password.web: Builds your Django app from the Dockerfile.depends_on: Tells Docker to start the database before starting the Django app.volumes: Maps your current folder (.) to the container folder (/app). This means if you change code on your laptop, it updates in the container instantly! Step 4: Connect Django to PostgresNow we need to tell Django to use this new Docker database instead of the default SQLite.Open settings.py and modify the DATABASES section: import os DATABASES = { 'default': { 'ENGINE': 'django.db.backends.postgresql', 'NAME': 'myproject_db', # Must match POSTGRES_DB in docker-compose 'USER': 'myuser', # Must match POSTGRES_USER 'PASSWORD': 'mypassword', # Must match POSTGRES_PASSWORD 'HOST': 'db', # The name of the service in docker-compose 'PORT': '5432', } } Step 5: Build and Run!Open your terminal in the project folder and run: docker-compose up --build You will see a lot of text scrolling by as Docker builds your environment. Once it stops, open your browser and go to http://localhost:8000.Wait, what about migrations? Since this is a fresh database, you need to create tables. In a new terminal tab, run: docker-compose exec web python manage.py migrate This command tells the "web" container to run the migrate command.Common Commands You NeedStop everything: Ctrl + C or docker-compose downRebuild after adding a new package: docker-compose up --buildCreate superuser: docker-compose exec web python manage.py createsuperuser ConclusionCongratulations! You have just containerized your Django application. No more installing Python versions manually or worrying about database conflicts. You can now share this project with anyone, and all they need to run it is docker-compose up.Happy Coding!  

Read Article →

Comments

0
?