You are writing a Python 3 utility that adds user accounts to hundreds of Linux servers. The script already works, but you now need to extend it so that it (1) hashes passwords with the bcrypt algorithm and (2) writes a YAML-formatted inventory file for an Ansible playbook. Both capabilities are provided by third-party libraries available on PyPI (bcrypt and PyYAML). Concerning how Python modules and packages should be brought into your project on a typical Linux workstation, which of the following statements is correct?
Because Python imports modules only from the directory that contains the running script, you must place the bcrypt and PyYAML folders next to your .py file or they will not load.
Create and activate a virtual environment for the project, then run pip install bcrypt PyYAML; the packages are placed in the environment's site-packages directory, isolated from the system Python and installable without root.
External packages cannot be imported until you recompile the Python interpreter with a --with-modules flag that statically links each library.
Manually copy the bcrypt and PyYAML source trees into /usr/lib/pythonX.Y so the interpreter can find them when the script runs.
The standard way to extend a Python script with third-party functionality is to create (or reuse) a virtual environment, activate it, and use pip to install the needed packages. The virtual environment's site-packages directory is separate from the global interpreter, so bcrypt and PyYAML are installed there without affecting system packages or requiring root privileges. Copying files directly into /usr/lib is unnecessary and fragile, recompiling the interpreter is not required to load external packages, and Python's import system searches many directories (including virtual-environment and system site-packages), not just the script's directory. Therefore, only the virtual-environment approach is correct.
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 a virtual environment in Python?
Open an interactive chat with Bash
How does pip install packages in a virtual environment?
Open an interactive chat with Bash
What benefits does using a virtual environment provide in Linux?
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