A practical guide to writing better Python scripts covering seven key practices: using inline dependencies with the PEP 723 script spec (compatible with uv and pip), safely handling secrets via environment variables, getpass, and the OS keyring, separating print output from logging, documenting env vars in --help, correctly routing output to stdout vs stderr, supporting stdin piping, returning proper exit codes, and loading configuration in a well-defined priority order. Each practice includes working code examples.
Table of contents
SummaryWhen it’s easy to write code, people doInline dependenciesDealing with secretsTo print or to log?Document your env varsstderr or stdout?Take it inMake a good exitMake it a priorityIt’s super annoying to do all that28.5K Impressions1 Comment