Python in production · mid
Running scripts reliably, logging failures and never unpickling untrusted data
A job needs the right interpreter and environment, logs that show a traceback and a record id, a non-zero exit code on failure, and no code execution from files that other people upload.
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceThe notes
What you must know
- Cron starts with a tiny environment: a short PATH, no shell profile and no activated venv. python3 is the system Python without your libraries. Call the venv interpreter by full path, or use uv run with the project path. sys.executable and sys.path show what really runs.
- Cron's working directory is the home folder, so relative paths fail. Build paths from Path(file).parent. Use a lock (flock) so runs do not overlap, save output to a log and alert on failure.
- Recent Debian and Ubuntu mark the system Python as externally managed (PEP 668), so pip refuses; do not force it.
- logger.error(f"Error occurred: {e}") keeps only the message. Use logger.exception("settlement failed for record %s", record_id) inside except to get the traceback. Log once at the boundary that handles the error.
- Silent nights: except Exception: pass; worker processes with spawn or forkserver do not inherit the parent's logging setup, so configure logging in an initializer; logging.basicConfig does nothing if the root logger already has a handler (use force=True); the root level defaults to WARNING.
- Use %s arguments in log calls, not f-strings, and never log card numbers or Aadhaar. Add context with a LoggerAdapter or a filter using contextvars, and prefer structured JSON logs.
- A script that swallows everything exits 0. It must exit non-zero on failure. Alert on job failure, on "rows settled" far below normal, and on a missing heartbeat.
- Unpickling can call any function, so a crafted file runs code as the service user. The pickle docs say never unpickle untrusted data. Use safetensors, ONNX, JSON, Parquet or Arrow, and sign artifacts built by CI. joblib also uses pickle. Also avoid yaml.load without SafeLoader, eval and exec on input, and subprocess with shell=True.
The red pen
Common traps: mistake → fix
Mistake: sudo pip install fixes ModuleNotFoundError under cron
Fix: it installs into the system Python and may break OS tools.
Mistake: Add more print calls to find a failure
Fix: use logger.exception with a record id.
Mistake: Only employees can upload, so pickle is fine
Fix: a stolen laptop or a wrong bucket permission is enough.
Mistake: We scan the pickle files, so it is safe
Fix: scanning does not make unpickling safe.
Easy to hard
Questions interviewers ask, easy to hard
- A script works by hand but cron says ModuleNotFoundError. Why?
- A job logs only "Error occurred" and no traceback. How do you fix the logging?
- Why can a job fail for three nights and exit 0?
- Why is pickle.load on an uploaded file critical?
- What safer formats do you use for models and data?
Explain it out loud
Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.
Practise by voiceYour first interview is free
Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.