Table of Contents

import sys

import sys is a Python import that provides access to interpreter state and command-line arguments: argv, exit(), stdin/stdout, and module paths. Use this for CLI scripts that need to read arguments or exit with a status code.

Example

# Python
# description: read command-line arguments, print to stderr
 
import sys
 
# Access command-line arguments (argv[0] is script name)
if len(sys.argv) < 2:
    print("Usage: script.py <name>", file=sys.stderr)
    sys.exit(1)
 
name = sys.argv[1]
print(f"Hello, {name}")
 
# Print Python version
print(f"Python {sys.version}")
 
sys.exit(0)  # exit with status 0 (success)

Common attributes and functions