PCAP Modules and Packages Practice Question
A script runs: import sys; print(sys.path[0]). The output is an empty string. What does this indicate?
⚠ Common exam trap
Python Institute often tests the subtle distinction between `sys.path[0]` being empty (stdin/`-c`) versus being the script's directory (file execution), and candidates confuse this with the current working directory or the `-I` flag's effect on `sys.path`.
Answer choices
Why each option matters
Answer the question above first, then reveal the full breakdown to understand why each option is right or wrong.
Correct answer & explanation
✓
The script is being read from stdin.
When a script is read from stdin (e.g., via `python < script.py` or `echo 'print(1)' | python`), Python sets `sys.path[0]` to an empty string because there is no script file path to derive the directory from. This is the documented behavior: `sys.path[0]` is the directory containing the script, or an empty string if the script is read from standard input.
Answer analysis
Option-by-option breakdown
For each option: why learners choose it and why it is or isn't the right answer here.
- ✓
The script is being read from stdin.
Why this is correct
When Python executes a script from standard input (for example, via `python < script.py`), there is no script directory to place at the front of `sys.path`. In that situation, CPython sets `sys.path[0]` to the empty string `''`, which the import system interprets as "search the current working directory." Because `print(sys.path[0])` prints that empty string, the output is a blank line, and the value itself is the documented signal that the script came from stdin. This is a deliberate design decision so that modules in the current directory remain importable even when no script file path exists.
- ✗
Python was launched with the -I flag.
Why it's wrong here
The `-I` flag enables isolated mode, which removes the script's directory (for script-file invocations) and user `site-packages` from `sys.path` and also suppresses environment variables. It does not, however, assign the empty string to `sys.path[0]` on its own; if a real script file path is supplied, `sys.path[0]` is still the absolute path of that script's directory. If stdin is used, the empty string comes from the stdin mechanism, not from `-I`. Therefore, `-I` cannot be the reason for an empty `sys.path[0]` when the source is a script file, and it is not a necessary condition for the observed output.
- ✗
The current working directory is not in sys.path.
Why it's wrong here
The empty string appearing as `sys.path[0]` is not an indication that the current working directory is missing from the module search path; in fact, it is exactly the opposite. An empty string is a special, explicit entry that tells Python to look in the current working directory during `import` operations. If the current working directory were truly absent from `sys.path`, the first element would be some other path (such as the script's directory) or the module search would start from a different explicit entry. Thus, the question's output confirms that the current working directory is included, not excluded, via the empty-string placeholder.
- ✗
The script is running from an interactive shell.
Why it's wrong here
In an interactive interpreter session, `sys.path[0]` is also commonly set to `''`, which would likewise print as a blank line, so the output alone is not sufficient to prove the source is interactive. However, the question explicitly states that a script is being run, and the term "script" generally refers to a file (or stdin stream) executed by the interpreter, not an interactive read-eval-print loop. The specific way to run a script without a file path is to feed it through stdin, and that is exactly the case where `sys.path[0]` becomes the empty string. Interactive mode is an alternative with the same `sys.path[0]` value, but it does not describe running a script, making it the wrong explanation.
Go deeper
Related to this question
About these practice questions
Courseiva writes every PCAP question from scratch — 169 in total, each with an explanation and a wrong-answer breakdown. None are copied from real exams or dumps. Learn why practice questions differ from exam dumps →
JA
Written by Johnson Ajibi, MSc IT Security
Senior Network & Security Engineer · founder of Courseiva
This PCAP practice question is part of Courseiva's free Python Institute certification practice question bank. Courseiva provides original exam-style practice questions with explanations, topic-based practice, mock exams, readiness tracking, and study analytics to help learners prepare for the PCAP exam.