import-sys
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
sys.argv: list of command-line argumentssys.exit(code): exit script with status codesys.stderr: standard error streamsys.stdout: standard output streamsys.stdin: standard input streamsys.version: Python version stringsys.platform: platform identifier (e.g., 'linux', 'darwin')sys.path: list of directories to search for modulessys.modules: dictionary of loaded modulessys.maxsize: maximum size of containers
import-sys.md · Last modified: by 127.0.0.1
