Code Reference

Sys

Python · Reference cheat sheet

Sys

Python · Reference cheat sheet


📋 Overview

sys exposes interpreter and process plumbing: CLI args, module search path, exit codes, and standard streams. Use it for lightweight CLIs; prefer argparse when flags grow.

🔧 Core concepts

APIRole
sys.argvCLI args: [script, ...]
sys.pathModule import search list
sys.exit(code)Exit process (0 ok, non-zero error)
sys.stdin / stdout / stderrText streams
sys.version / sys.version_infoRuntime version
sys.platformwin32, linux, darwin, …
sys.executablePath to this Python binary
sys.getsizeof(obj)Approx. object size (bytes)

💡 Examples

argv:

import sys

# python tool.py a b
script, *args = sys.argv
print("script:", script)
print("args:", args)
if not args:
    sys.exit("usage: tool.py <files...>")

path and version:

import sys
from pathlib import Path

print(sys.version_info[:3])
print(sys.executable)
# Add local package root for scripts (prefer installable packages):
root = Path(__file__).resolve().parent
if str(root) not in sys.path:
    sys.path.insert(0, str(root))

stdin / stdout:

import sys

for line in sys.stdin:
    sys.stdout.write(line.upper())
# echo hello | python upper.py

exit codes:

import sys

ok = True
sys.exit(0 if ok else 1)
# or: raise SystemExit(2)

⚠️ Pitfalls

  • sys.argv[0] is the script path (or -c / -m), not always a useful name.
  • Mutating sys.path is a last resort — prefer packages / PYTHONPATH / editable installs.
  • sys.exit("msg") prints to stderr and exits with code 1.
  • Redirected stdout may be block-buffered — flush or use print(..., flush=True).
  • Don’t compare versions as strings; use sys.version_info >= (3, 11).

On this page