KatInterview

Walking Through a Python venv Bootstrap Shell Script

System DesignModel answer
MediumModel answerSystem Designbashpython-venvpip

Last updated

Explain each line of a bash script that builds a Python virtual environment, predict its output and files, and argue why shell automation suits environment setup.

You are handed the following setup script and asked to talk through it as you would with a teammate.

#!/usr/bin/env bash
set -euo pipefail

mkdir -p demo_app
cd demo_app

python3 -m venv .venv
source .venv/bin/activate

pip install --upgrade pip
pip install requests

pip freeze > requirements.txt
echo "Ready: $(python --version) at $(which python)"

Answer the following:

  1. Explain what every line does and why it is there.
  2. Suppose you save the file as setup.sh and run bash setup.sh in a terminal. Describe the output you would see and the files and directories left behind.
  3. Is your terminal still in the virtual environment afterward? Explain why or why not.
  4. What are the advantages of using a shell script for environment setup and automation, and where does this approach fall short?

State your assumptions (OS, Python version, network access) and mention failure modes you would guard against.

Hints

Hint 1

Go line by line, but group them: interpreter and safety settings, directory creation, venv creation and activation, package installation, and recording and reporting.

Hint 2

Think about which process executes the script when you type `bash setup.sh`. What happens to environment changes such as PATH modifications when that process exits?

Hint 3

For the advantages part, consider repeatability, speed of onboarding, and use in CI, then name limits such as Windows support, unpinned versions, and growing complexity.