Introduction π
The sys module provides access to variables, functions, and objects that interact directly with the **Python interpreter**. It is essential for tasks involving command-line arguments, runtime control, environment details, I/O redirection, and system-specific information.
Note
π‘ Most useful for CLI tools, automation scripts, debugging, and interpreter control
1. Importing the sys Module π§±
import_sys.py
import sysβ Now you can interact with Python internals
2. Command-Line Arguments β sys.argv π―
sys_argv.py
import sys
print(sys.argv) # list of command-line argumentsβ sys.argv[0] β script name
β sys.argv[1] β first argument passed
Example: terminal usage
Code Snippet
python app.py 10 20Output:
Code Snippet
['app.py', '10', '20']Note
3. Exiting the Program β sys.exit() πͺ
sys_exit.py
print("Before exit")
sys.exit()
print("This will not run")β Stops program execution immediately
β Can pass an exit code: sys.exit(1)
4. Python Version Info β sys.version π
version_info.py
print(sys.version)
print(sys.version_info)β Helpful for environment debugging
5. Path Management β sys.path π£οΈ
sys_path.py
print(sys.path)β Shows directories Python searches for modules
β You can modify sys.path to import from custom folders
add_path.py
sys.path.append("C:/my_modules")Note
6. Standard Input, Output, Error Streams π§΅
Standard Output β sys.stdout
stdout_example.py
sys.stdout.write("Hello World\n")Standard Error β sys.stderr
stderr_example.py
sys.stderr.write("This is an error message\n")Standard Input β sys.stdin
stdin_example.py
data = sys.stdin.read()
print(data)β Useful for automation, pipelines, batch processing
7. Get System Platform Info π₯οΈ
platform_info.py
print(sys.platform)β Returns OS type (win32, linux, darwin)
8. Recursion Limit β sys.getrecursionlimit() π
recursion_limit.py
print(sys.getrecursionlimit())
sys.setrecursionlimit(2000)Note
9. Get Interpreter Information π
executable_path.py
print(sys.executable) # Python interpreter pathsys_prefix.py
print(sys.prefix) # Virtual environment/base path10. Memory Size of an Object β sys.getsizeof() π§
sizeof_example.py
print(sys.getsizeof(1000)) # bytesβ Useful for optimization & memory profiling
11. Real-World Example β CLI Calculator π―
cli_calculator.py
import sys
if len(sys.argv) < 3:
print("Usage: python calc.py num1 num2")
sys.exit()
n1 = int(sys.argv[1])
n2 = int(sys.argv[2])
print("Sum:", n1 + n2)β Demonstrates command-line argument handling
12. Real-World Example β Logging Errors to stderr π¨
stderr_logging.py
import sys
try:
x = 10 / 0
except Exception as e:
sys.stderr.write(f"Error: {e}\n")13. SYS Module Cheat Sheet π
| Feature | Function |
|---|---|
| Arguments | sys.argv |
| Exit program | sys.exit() |
| Python version | sys.version |
| Module search paths | sys.path |
| Recursion limit | sys.getrecursionlimit() |
| Object size | sys.getsizeof() |
| Interpreter path | sys.executable |
| OS platform | sys.platform |
Best Practices π‘
- β Use sys.argv for command-line apps
- β Modify sys.path cautiously
- β Send debug messages to stderr
- β Avoid increasing recursion limit unless necessary
- β Use sys.exit() for controlled program termination
Conclusion π
You now fully understand the sys module! Want the next topic? Try Pathlib, Shutil, Subprocess, or Argparse. Just tell me! π