A Linux administrator is writing a Python 3 script that will run unattended on several distributions through cloud-init. The script must be able to (1) discover the absolute path of the interpreter that is executing it, (2) append the directory "/opt/tools" to the module search path so that import statements can find locally packaged utilities, and (3) terminate with exit status 2 if a validation step fails. Which single built-in Python module supplies attributes or functions to carry out all three of these tasks without adding any external dependencies?
The sys module is part of the Python standard library and is automatically available in every Python program.
sys.executable returns the full path to the currently running interpreter binary, satisfying the first requirement.
sys.path is a mutable list that Python consults when importing modules; appending "/opt/tools" to this list lets the interpreter locate additional packages at runtime.
sys.exit(2) raises SystemExit with status code 2, allowing the script to terminate with the desired exit value. Other options do not meet all three needs: os lacks a direct equivalent of sys.path; platform can report interpreter details but cannot modify the import path or exit; subprocess is intended for spawning external commands, not for introspection of the running interpreter.
Ask Bash
Bash is our AI bot, trained to help you pass your exam. AI Generated Content may display inaccurate information, always double-check anything important.
What is the purpose of the `sys.executable` attribute in Python?
Open an interactive chat with Bash
How does modifying `sys.path` affect Python imports?
Open an interactive chat with Bash
What happens when you call `sys.exit(2)` in a Python script?
Open an interactive chat with Bash
CompTIA Linux+ XK0-006 (V8)
Automation, Orchestration, and Scripting
Your Score:
Report Issue
Bash, the Crucial Exams Chat Bot
AI Bot
Loading...
Loading...
Loading...
IT & Cybersecurity Package Join Premium for Full Access