Why is `pip freeze > requirements.txt` not a great lock strategy?
IntermediateConceptEnvironments, dependencies & packaging
Answer
It dumps whatever happens to be installed in the current environment, including leftovers from experiments and tools like pytest or ipython, with no record of which packages you actually depend on directly. It has no hashes and no separation between prod and dev dependencies. Better: keep direct dependencies in pyproject.toml or requirements.in, compile them into a pinned, hashed lock (pip-compile --generate-hashes), and install with pip install --require-hashes -r requirements.txt.
More on Environments, dependencies & packaging
What is a virtual environment and why do we always use one?BeginnerWhat does `error: externally-managed-environment` mean?Beginner`requirements.txt` vs `pyproject.toml`: what goes where?IntermediateWhat do version specifiers like `~=`, `==`, `>=,<` mean?BeginnerWhat are `pipx` and `uv`, and when do you use them?IntermediateHow do you manage multiple Python versions on one machine?Intermediate