Interview Prep/Python for DevOps/Environments, dependencies & packaging

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.

All Python for DevOps questions Roadmap: Python for automation

More on Environments, dependencies & packaging