Harnessing `sys`
Standard Library
sys Module Primer
sys exposes the guts of the running Python interpreter—perfect for CLI tooling, logging, and environment checks.
argv & exit codes
import sys
print(sys.argv) # raw command-line arguments
if '--help' in sys.argv:
sys.exit(0)
- Pair
sys.argvwith argparse for fully featured CLIs. - Use
sys.exit(code)instead ofquit()for explicit termination.
Interpreter settings
| Task | Snippet |
|---|---|
| Python version | sys.version_info |
| Recursion limit | sys.getrecursionlimit() / sys.setrecursionlimit() |
| Default encoding | sys.getdefaultencoding() |
| Standard streams | sys.stdin, sys.stdout, sys.stderr |
Paths & modules
- Inspect import search path via
sys.path. - Append custom directories for tooling:
sys.path.append('src'). - Hook into import system with
sys.meta_pathfor advanced use cases.