What is a virtual environment and why do we always use one?
BeginnerConceptEnvironments, dependencies & packaging
Answer
A venv is an isolated directory with its own python symlink, pip and site-packages. Each project gets its own dependency versions, nothing touches the system Python that the OS (and tools like apt, yum, cloud-init) depend on, and the environment can be deleted and rebuilt at will.
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
deactivate
Activation only edits PATH; you can skip it and call .venv/bin/python directly, which is what cron jobs and systemd units should do.
More on Environments, dependencies & packaging
What does `error: externally-managed-environment` mean?Beginner`requirements.txt` vs `pyproject.toml`: what goes where?IntermediateWhy is `pip freeze > requirements.txt` not a great lock strategy?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