sys.argv / sys.exit() / sys.path
Attributes and functions of the sys module, which manages command-line arguments, script termination, and module search paths.
Syntax
import sys # List of command-line arguments (index 0 is the script name) sys.argv # Exits the script (you can specify an exit code). sys.exit(0) # Normal exit sys.exit(1) # Error exit # List of directories searched for modules sys.path # Python version string sys.version # Platform identifier of the runtime environment sys.platform
Attributes and Functions
| Attribute / Function | Description |
|---|---|
| sys.argv | A list of command-line argument strings. sys.argv[0] is the script name; subsequent elements are the arguments passed by the user. |
| sys.exit(code) | Exits the script. An exit code of 0 indicates success; a value of 1 or greater indicates an error. |
| sys.path | A list of directories that Python searches for modules. You can add a path to this list to make your own modules importable. |
| sys.version | Returns version information about the running Python interpreter as a string. |
| sys.version_info | Returns version information as a named tuple. Useful for comparing version numbers. |
| sys.platform | Returns a string identifying the runtime platform (e.g., 'linux', 'darwin', 'win32'). |
| sys.stdin / sys.stdout / sys.stderr | File objects for standard input, standard output, and standard error. |
Sample Code
import sys
# Use sys.argv to access command-line arguments.
# When run as: python script.py hello 42
print(sys.argv[0]) # Script name: prints 'script.py'
print(sys.argv[1]) # First argument: prints 'hello'
print(sys.argv[2]) # Second argument: prints '42'
print(len(sys.argv)) # Number of arguments: prints '3'
# Check for missing arguments
if len(sys.argv) < 2:
print('Usage: python script.py <filename>', file=sys.stderr)
sys.exit(1) # Exit with an error code.
# Receive and process the argument.
filename = sys.argv[1]
print(f'Processing file: {filename}')
# Add a path to sys.path to make a module importable.
sys.path.append('/path/to/my/modules')
# Check the Python version with sys.version.
print(sys.version) # e.g., prints '3.12.0 (main, ...)'
# Use version_info for version comparisons.
if sys.version_info >= (3, 10):
print('Python 3.10 or later: match statements are available.')
# Use sys.platform to detect the runtime environment.
if sys.platform == 'win32':
print('Running on Windows.')
elif sys.platform == 'darwin':
print('Running on macOS.')
else:
print('Running on Linux.')
# Write a message to standard error.
print('An error occurred.', file=sys.stderr)
Notes
sys.argv is the fundamental mechanism for passing arguments to a script from the command line. All arguments are stored as strings, so convert them with int() or float() when numeric values are needed. For more complex argument parsing, the standard library's argparse module makes it easy to implement automatic help messages, type conversion, and optional arguments.
sys.exit() internally raises a SystemExit exception, which means it can be caught with try/except. In most cases, however, you should let it propagate to terminate the script normally. Exit codes are used by shell scripts and CI/CD systems to determine success or failure. Always exit with a non-zero code when an error occurs.
For reading environment variables, see os.environ / os.getenv().
If you find any errors or copyright issues, please contact us.