Skip to main content

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.argv with argparse for fully featured CLIs.
  • Use sys.exit(code) instead of quit() for explicit termination.

Interpreter settings

TaskSnippet
Python versionsys.version_info
Recursion limitsys.getrecursionlimit() / sys.setrecursionlimit()
Default encodingsys.getdefaultencoding()
Standard streamssys.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_path for advanced use cases.

Next up in your learning path