Your Python tool takes 9 seconds just to print `--help`. Users complain. What do you do?
AdvancedScenarioAutomation scripting & CLI design
Answer
Situation read → Heavy top-level imports or work at import time (clients created, network calls).
What I'd do →
- Measure:
python -X importtime -m tool --help 2>&1 | sort -t'|' -k2 -n | tail -20. - Move expensive imports (boto3, kubernetes, pandas) inside the subcommand functions that use them.
- Remove any import-time side effects: no clients, config loading or API calls at module level.
- Check the venv is not on a slow network filesystem.
Why → --help should cost only argument parsing; lazy imports make every command start fast.
Interview tip → -X importtime is the tool to name.
More on Automation scripting & CLI design
Your team has a 300-line Bash script that parses JSON with `jq`, loops over AWS accounts and retries with `sleep`. It breaks every month. Do you rewrite it in Python?BeginnerA colleague's script deletes files older than 30 days from `/var/log/app`. You are asked to review it before it goes into cron. What do you look for?IntermediateYou need a script that ops engineers run from laptops and that CI runs nightly. How do you design its interface?IntermediateA script reads a 15 GB access log to count 5xx errors per endpoint, and the box runs out of memory. How do you fix it?BeginnerYou must run the same Python automation on 40 servers that have Python 3.6, 3.9 and 3.12. What do you do?IntermediateA cron job that runs a Python script fails silently. Nobody noticed for two weeks. How do you make sure that never happens again?Intermediate