Module 15 · Python Standard Libraries — topic 6 of 13
sys
The sys module: command-line arguments with sys.argv, exiting with sys.exit and exit codes, standard streams, the Python version, sys.path and sys.modules, and the recursion limit.
Learning objectives
- Read command-line arguments
- Exit with meaningful exit codes
- Inspect the interpreter's version and settings
Definition
sys gives access to the Python interpreter itself: the arguments the program was started with, the standard input/output/error streams, the version, the module search path, loaded modules and interpreter settings.
Introduction
Command-line tools receive arguments, print errors separately from output, and report success or failure with an exit code. sys provides all three.
Why it matters
Well-behaved command-line programs follow these conventions, so they work with shells, scripts and automation tools.
Detailed explanation
sys.argv is a list: argv[0] is the script name and the rest are the arguments typed after it (all strings).
sys.exit(code) ends the program by raising SystemExit. Exit code 0 means success; non-zero means an error. sys.exit("message") prints the message to stderr and exits with code 1.
sys.stdin, sys.stdout and sys.stderr are the standard streams. Error messages should go to stderr: print(msg, file=sys.stderr).
sys.version and sys.version_info describe the running Python; compare with tuples: sys.version_info >= (3, 10).
sys.path is the module search path and sys.modules the cache of loaded modules (see Modules and Packages).
sys.getrecursionlimit() returns the maximum recursion depth (usually 1000).
For anything beyond a couple of arguments, use argparse, which builds on sys.argv and generates help messages.
Syntax
- argv holds arguments as strings.
- exit codes signal success or failure.
- Errors go to stderr.
Code examples
Interpreter information
True 3 list
Streams and exiting
written directly 17 exit code: 3
More examples
A tiny command-line program
Hello, Asha! Hello, Asha! exit 0 usage: greet NAME [TIMES] exit 2 True True
Step-by-step explanation
- main receives the argument list instead of reading sys.argv directly, which makes it easy to test.
- It returns an exit code; a real script would end with sys.exit(main(sys.argv)).
- The usage message is printed to stdout here so the example output is visible; real tools send it to stderr.
Expected output
Running “A tiny command-line program” prints exactly:
Practical applications
- Command-line utilities and automation scripts.
- Checking the Python version before using new features.
Best practices
- Return exit codes from main() and pass them to sys.exit.
- Print errors to stderr.
- Use argparse for real argument parsing.
Common mistakes
Forgetting argv items are strings
Avoid
Prefer
Arguments arrive as text and must be converted.
Catching SystemExit by accident
Avoid
Prefer
BaseException includes SystemExit, so the program can't exit properly.
Important notes
- sys.stdin.read() reads all piped input, e.g. from python script.py < data.txt.
Key takeaways
- sys.argv holds command-line arguments.
- sys.exit(code) ends with an exit code.
- sys exposes the version, streams, path and modules.
Try it yourself
- 1Write add.py that prints the sum of all numbers given as arguments.
- 2Make a script exit with code 1 and an error message on stderr when no file name is given.
Finished reading? Mark it complete to update your dashboard.
Suggested practice
1 question test this exact topic. Answers and explanations are revealed after you submit.
- ModerateWhat is sys.argv[0]?