ReviseAlgo Logo

Modules & Packages

sys Module

System-specific parameters and Python runtime environment

Interview: Python internals — argv, recursion limits, and module system are tested

Last Updated: June 12, 2026 7 min read

The sys module provides access to system-specific parameters and functions that interact with the Python interpreter. It's essential for command-line argument handling, understanding Python's runtime environment, and controlling interpreter behavior.

Key sys Attributes

  • sys.argv: List of command-line arguments — argv[0] is the script name
  • sys.path: List of directories Python searches for modules
  • sys.modules: Dict of all loaded modules — cache of imported modules
  • sys.version: Python version string
  • sys.platform: Platform identifier ("linux", "darwin", "win32")
  • sys.stdout/stderr/stdin: Standard I/O streams

Controlling the Interpreter

  • sys.exit(code): Exit the program with a status code (0 = success)
  • sys.setrecursionlimit(n): Change max recursion depth (default ~1000)
  • sys.getsizeof(obj): Get memory size of an object in bytes
  • sys.getrefcount(obj): Get reference count (for debugging memory)

sys.argv vs argparse

For simple scripts, sys.argv works. For anything with flags, options, or help text, use argparse or click instead. They handle validation, help messages, and type conversion automatically.

Use Cases

Building command-line tools with argument parsing

Debugging memory usage with sys.getsizeof()

Writing platform-specific code for cross-platform applications

Understanding and modifying the module import search path

Controlling interpreter behavior (recursion limits, exit codes)

Common Mistakes

Using sys.argv for complex CLI — argparse or click handles validation and help text automatically

Increasing recursion limit without understanding the consequences — can cause segfaults

Forgetting sys.argv[0] is the script name — actual arguments start at argv[1]

Not using sys.exit() for proper exit codes — scripts should return 0 for success, non-zero for errors

Confusing sys.getsizeof with total memory — it only measures the object itself, not referenced objects