Skip to main content

Creating Your requirements.txt File

The requirements.txt file is a standard way to define dependencies for your Python project. It ensures consistency across development environments by listing all external packages your project relies on. Follow these steps to create and manage it effectively. Using a virtual environment helps isolate your project dependencies, preventing conflicts with global packages.

Activating a Virtual Environment

  • Windows:
    python -m venv venv
    .\venv\Scripts\activate
    
  • Linux/macOS:
    python3 -m venv venv
    source venv/bin/activate
    

Step 2: List Dependencies

You can manually list your required packages in requirements.txt, but the easiest way is to generate it using pip freeze.

Automatically Generate requirements.txt

Once you’ve installed your dependencies inside the virtual environment, run:
pip freeze > requirements.txt
This will create a requirements.txt file with all installed packages and their versions.

Best Practices

Warning: Only include necessary dependencies (e.g., discord.py) to avoid unnecessary bloat in production.

Example requirements.txt File

flask
requests
discord.py

Step 3: Review and Edit

Open requirements.txt in a text editor and remove any unnecessary dependencies to keep your project clean.

Step 4: Use and Share

Distribute requirements.txt to collaborators or use it to set up your project on another system.

Install Dependencies from requirements.txt

To recreate the environment on a new machine, run:
pip install -r requirements.txt

Bonus Tips

  • Regular Updates: Update requirements.txt whenever dependencies change.
  • Avoid Global Environments: Always run pip freeze inside a virtual environment to avoid including unrelated packages.
  • Compatibility with DS Cloud: If using pyproject.toml or Pipfile, generate a requirements.txt for compatibility.
A well-maintained requirements.txt file ensures a smooth development and deployment experience.