Python Reference ยท Files, Paths and Text I/O
The sys module
Reference for Python's sys module: command-line arguments, exit codes, standard streams, module search path, version info, recursion limit and sizes.
The sys module gives your program access to the running interpreter: the command-line arguments, the standard input, output and error streams, the module search path, the Python version, and a handful of limits and hooks. It is always available and needs only import sys.
Most of what sys offers is data (attributes you read, and sometimes assign) rather than functions. A few attributes are used in almost every script, such as sys.argv, sys.exit() and sys.stdout; the rest are for tools, debuggers and special cases.
import sys print(sys.version_info[:2]) sys.stdout.write("no newline added\n")
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.
Frequently used names
| Name | Description |
|---|---|
sys.argv |
List of command-line arguments as strings. argv[0] is the script name. For anything beyond trivial parsing, use argparse. |
sys.exit(arg=None) |
Raises SystemExit. 0 or None means success, another integer is the exit status, and a string is printed to stderr with status 1. |
sys.stdin, sys.stdout, sys.stderr |
The standard streams as text file objects. print() writes to stdout, input() reads from stdin. |
sys.path |
List of directories searched when you import. Modified by PYTHONPATH and by virtual environments. |
sys.modules |
Dictionary of modules already imported. A second import of the same name just looks it up here. |
sys.version, sys.version_info |
Full version string; a named tuple (major, minor, micro, releaselevel, serial) suited to comparisons. |
sys.platform |
A short string such as "linux", "darwin", "win32" or "emscripten" (Python in the browser). |
sys.executable |
Path of the Python interpreter running the program. |
sys.getsizeof(obj) |
Shallow size of an object in bytes. |
sys.getrecursionlimit(), sys.setrecursionlimit(n) |
Read or change the maximum recursion depth. |
sys.maxsize |
The largest value of a C Py_ssize_t, usually 2**63 - 1. Not a limit on int. |
sys.exc_info(), sys.exception() |
Information about the exception being handled; sys.exception() (3.11+) returns just the exception object. |
sys.flags, sys.implementation, sys.float_info, sys.int_info |
Details about interpreter options, the implementation, and number representations. |
Usage notes
sys.exit()raises an exception,SystemExit, which is whyfinallyblocks and context managers still run. A bareexcept:would swallow it; catchExceptioninstead. Threads that callsys.exit()only end that thread.- Exit codes matter to shells and CI:
0is success, anything else is failure. An uncaught exception ends the program with code 1. print()is a thin layer oversys.stdout. Replacingsys.stdoutcaptures prints, which is useful in tests, butcontextlib.redirect_stdout()does the same job with automatic cleanup.sys.argvitems are always strings. Convert numbers yourself and checklen(sys.argv)first, or you getIndexError.sys.getsizeof()is shallow. A list of a million large objects reports only the list's own pointer array. It also includes garbage-collector overhead and its exact numbers differ between Python versions and platforms, so never hard-code them.- Raising the recursion limit can crash the interpreter with a real stack overflow if you go too far. Prefer loops or an explicit stack for deep algorithms.
- Do not edit
sys.pathas a first resort. Proper packaging, or running withpython -m package.module, avoids most path hacks. sys.version_infocomparisons are the right way to branch on versions. Comparingsys.versionstrings gives wrong answers ("3.9" > "3.10"is true as text).- Platform dependent.
sys.platformandsys.executablediffer in each environment; the examples above avoid printing them.
Related
os,argparse,platform,print(),input()- Lessons: Command-line programs, Input and output, Recursion