Site Tools


import-sys

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

  • sys.argv: list of command-line arguments
  • sys.exit(code): exit script with status code
  • sys.stderr: standard error stream
  • sys.stdout: standard output stream
  • sys.stdin: standard input stream
  • sys.version: Python version string
  • sys.platform: platform identifier (e.g., 'linux', 'darwin')
  • sys.path: list of directories to search for modules
  • sys.modules: dictionary of loaded modules
  • sys.maxsize: maximum size of containers
import-sys.md · Last modified: by 127.0.0.1