Skip to main content
The sys module provides access to variables and functions that interact closely with the Python interpreter.

Module Import

Common Attributes

sys.argv

Command-line arguments passed to a Python script.

sys.path

List of directories Python searches for modules.

sys.platform

Platform identifier.

sys.version

Python version information.

Standard Streams

sys.stdin, sys.stdout, sys.stderr

Standard input, output, and error streams.

Exit Functions

sys.exit()

Exit the program.
int | str
default:"0"
Exit status code (0 for success, non-zero for error) or exit message

sys.exc_info()

Get information about current exception.

Module and Import Functions

sys.modules

Dictionary of loaded modules.

sys.meta_path

List of meta path finders.

Size and Memory

sys.getsizeof()

Get size of an object in bytes.
any
required
Object to get size of
int
Size in bytes

sys.getrecursionlimit() / sys.setrecursionlimit()

Get or set maximum recursion depth.

System Information

sys.maxsize

Maximum value a variable of type Py_ssize_t can take.

sys.byteorder

Byte order of the system.

sys.executable

Path to Python interpreter.

Debugging and Profiling

sys.settrace()

Set trace function for debugging.

sys.setprofile()

Set profiling function.

Flags and Options

sys.flags

Command-line flags.

Practical Examples

Command-Line Script

Platform-Specific Code

Progress Bar with stderr

Memory Usage Tracker

Complete Reference

Constants

  • sys.platform - Platform identifier
  • sys.byteorder - Native byte order (‘little’ or ‘big’)
  • sys.maxsize - Maximum Py_ssize_t value
  • sys.maxunicode - Maximum Unicode code point
  • sys.version - Version string
  • sys.version_info - Version tuple
  • sys.hexversion - Version as single number
  • sys.api_version - C API version
  • sys.executable - Python interpreter path
  • sys.prefix - Installation prefix
  • sys.base_prefix - Base installation prefix
  • sys.exec_prefix - Platform-specific installation prefix

Functions

  • sys.exit(status) - Exit the interpreter
  • sys.excepthook(type, value, traceback) - Handle uncaught exceptions
  • sys.exc_info() - Current exception information
  • sys.last_type, sys.last_value, sys.last_traceback - Last exception
  • sys.settrace(func) - Set trace function
  • sys.gettrace() - Get trace function
  • sys.setprofile(func) - Set profile function
  • sys.getprofile() - Get profile function
  • sys.getrefcount(object) - Reference count
  • sys.getsizeof(object) - Object size in bytes
  • sys.intern(string) - Intern string

Best Practices

Use sys.exit() for clean exits:
Be careful with sys.setrecursionlimit(): Setting it too high can cause stack overflow. Python’s default limit (usually 1000) is reasonable for most cases. If you need deeper recursion, consider iterative approaches instead.
sys.argv is always a list: Always check length before accessing elements to avoid IndexError.

os

Operating system interfaces

platform

Platform identification

argparse

Command-line argument parsing

logging

Logging facility