Interview Prep/Build Tools (Java, Node.js, Python, .NET)/Python builds (pip, venv, requirements.txt)
What is requirements.txt and how do you pin versions?
BeginnerConceptPython builds (pip, venv, requirements.txt)
Answer
Short answer
requirements.txt lists the Python packages an app needs, one per line, for pip install -r requirements.txt. Pinning means writing exact versions with == so every install gets the same packages. pip freeze > requirements.txt captures everything currently installed with exact versions.
Example
flask==3.0.3
gunicorn==22.0.0
SQLAlchemy==2.0.31
psycopg2-binary==2.9.9
requests>=2.31,<3 # range: allowed but less predictable
Good practice: two files
requirements.in # what you directly need (loose)
flask
gunicorn
requirements.txt # generated, everything pinned incl. transitive deps
pip-compile requirements.in (from pip-tools)
Many teams also keep requirements-dev.txt for pytest, ruff, mypy.
Hash checking (extra safety)
pip-compile --generate-hashes requirements.in
pip install --require-hashes -r requirements.txt # refuses tampered packages
What to say in the interview
"requirements.txt is the dependency list for pip. For reproducible builds we pin exact versions, ideally generated with pip-compile so transitive dependencies are pinned too, and keep dev tools in a separate file."
All Build Tools (Java, Node.js, Python, .NET) questions Roadmap: Python apps: pip, venv & requirements.txt
More on Python builds (pip, venv, requirements.txt)
How do you install dependencies and run a Python application?BeginnerWhat is a Python virtual environment and why do we need it?BeginnerHow do you run a Flask application in development and in production?BeginnerWhat is gunicorn and how many workers should you run?BeginnerWhat is the difference between WSGI and ASGI?IntermediateHow do you run a FastAPI application?Beginner