In this lesson, you will learn how to optimize your project by separating the libraries your application needs to run from the tools you only use while writing code.
The Problem of Bloated Environments
As your projects grow, you accumulate tools for testing (like pytest), linting (flake8), or formatting (black). If you bundle these into your production environment, you introduce unnecessary security risks, bloat your deployment images, and increase the likelihood of dependency conflicts. A clean production environment should contain only the dependencies required for your code to execute.
Defining Dependency Groups
To manage this, you must categorize your dependencies into production (core) and development (dev).
While a basic requirements.txt file is often used for everything, modern tools like Poetry or uv allow you to define groups. If you are using a standard pip workflow, it is common practice to maintain two separate files: requirements.txt for production and requirements-dev.txt for local development.
Practical Implementation
Consider a project where your application needs requests to function, but you only need pytest to verify your logic. You would structure your environment like this:
requirements.txt:
requests==2.31.0
requirements-dev.txt:
-r requirements.txt
pytest==8.0.0
black==24.1.0
By adding -r requirements.txt at the top of your dev file, you ensure that your development environment includes everything production has, plus your testing toolkit. When deploying to a server, you install only the core requirements:
pip install -r requirements.txt
When setting up your machine to work on the project, you install the full set:
pip install -r requirements-dev.txt
This approach keeps your production footprint lean and ensures that your testing tools are strictly confined to the environments where they are actually executed. By keeping these layers distinct, you prevent local tooling configurations from leaking into your production runtime, leading to more stable and predictable deployments.
Key Takeaway: Always segregate your dependencies by purpose, ensuring that your production environment remains lightweight and free of unnecessary development and testing overhead.