Learn sys

The interpreter's own controls: arguments, streams, exit codes and runtime state

13 lessons · 2 hours · 4 levels · covers sys Python 3.13

The sys module exposes the running Python interpreter: command-line arguments, the standard streams, exit codes, the module search path, version and platform data, and hooks for tracing, auditing and exceptions. Command-line tools, launchers, debuggers and profilers are built on it, and knowing it explains a lot about how Python itself runs your code.

Before you start

sys builds on these; the tree links to the exact lessons you need.

How deep do you want to go?

1. EssentialsRead and write simple code, do the everyday basics4 lessons · 30 min in total
2. PracticalUse it productively at work, with the usual idioms7 lessons · 1 hour in total
3. AdvancedThe less common features, performance and trade-offs10 lessons · 1 hour in total
4. ExpertInternals and the edge cases experts know13 lessons · 2 hours in total

The sys tree

sysCommand-line arguments with sys.argvCommand-linearguments with sys…stdin, stdout and stderrstdin, stdout andstderrExit codes and sys.exitExit codes andsys.exitVersion and platform checksVersion and platformcheckssys.path and sys.modulessys.path andsys.modulesWriting robust pipeline toolsWriting robustpipeline toolsCurrent exceptions and sys.excepthookCurrent exceptionsand sys.excepthookReading and writing filesReading and writingfilesErrors and try/exceptErrors and try/exceptModules and importsModules and importsExceptions in depthExceptions in depth
Essentials and Practical shown (7 of 13 lessons); the full list is below. Click a lesson to read it.

All 13 lessons, in order

Command-line programs

  1. Command-line arguments with sys.argv · Essentials · 7 min

    Read the arguments a script was started with from sys.argv.

  2. stdin, stdout and stderr · Essentials · 9 min

    Read input from sys.stdin, write results to sys.stdout and diagnostics to sys.stderr.

  3. Writing robust pipeline tools · Practical · 11 min

    Make a script behave like a proper Unix filter: stream stdin, handle encodings and binary data, and exit cleanly on a closed pipe.

Errors and exits

  1. Exit codes and sys.exit · Essentials · 7 min

    End a program with sys.exit, return meaningful exit codes, and understand that it works by raising SystemExit.

  2. Current exceptions and sys.excepthook · Practical · 10 min

    Inspect the exception being handled with sys.exception() and customise how uncaught exceptions are reported.

The interpreter and its environment

  1. Version and platform checks · Essentials · 7 min

    Find out which Python and operating system you are running on with sys.version_info, sys.platform and sys.executable.

  2. sys.path and sys.modules · Practical · 11 min

    See where imports are searched for with sys.path and which modules are already loaded in sys.modules.

  3. Virtual environments: prefix and base_prefix · Advanced · 8 min

    Detect whether code runs inside a virtual environment and where its packages come from.

Memory and limits

  1. Recursion limit and stack depth · Advanced · 10 min

    Understand sys.getrecursionlimit, why RecursionError exists, and when raising the limit is safe.

  2. Object sizes with sys.getsizeof · Advanced · 9 min

    Measure how many bytes an object occupies with sys.getsizeof, and why the answer is shallow.

  3. Reference counts and interning · Expert · 12 min

    Observe CPython's reference counting with sys.getrefcount, immortal objects, and string interning with sys.intern.

Hooks and introspection

  1. Tracing and monitoring execution · Expert · 14 min

    How debuggers, profilers and coverage tools observe code: sys.settrace, sys.setprofile and the low-overhead sys.monitoring API.

  2. Audit hooks · Expert · 11 min

    Observe security-relevant runtime events such as file opens, imports and subprocess launches with sys.addaudithook.