Python Module Search Path: sys.path and PYTHONPATH
Which THREE of the following statements about Python's module search path are true?
Quick Answer
The answer is that modifying sys.path at runtime is a valid way to change Python's module search path. This is correct because sys.path is simply a list of directory strings that Python traverses in order when resolving import statements, and like any Python list, it can be appended, inserted, or reassigned during program execution. The PYTHONPATH environment variable works differently—it is read only at interpreter startup and its contents are prepended to sys.path automatically, making it a static, pre-runtime configuration tool. On the Certified Associate Python Programmer PCAP exam, this distinction tests your understanding of Python’s import system and the difference between runtime and environment-level configuration. A common trap is assuming PYTHONPATH can be changed mid-session, but it only affects new interpreter instances. Memory tip: think of sys.path as a live shopping list you can edit at the store, while PYTHONPATH is the list you bring from home before you leave.
⚠ Common exam trap
Python Institute often tests the exact order of module search path components, and the trap here is that candidates mistakenly believe site-packages is searched before PYTHONPATH, or that the current working directory is always last, when in fact the script's directory is first and PYTHONPATH precedes site-packages.
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 PYTHONPATH environment variable can be used to add custom directories to sys.path.
Option A is correct because PYTHONPATH is an environment variable whose directories are prepended to the default module search path, effectively extending sys.path so custom modules can be imported. Option C is correct because when a script is executed, Python automatically inserts the directory containing that script at the front of sys.path, allowing sibling modules to be imported. Option E is correct because sys.path is an ordinary Python list that can be modified at runtime (e.g., via sys.path.append() or sys.path.insert()) to alter where subsequent imports are resolved. Option B is wrong because PYTHONPATH directories are searched before site-packages, not after. Option D is wrong because the current working directory is not always the last entry in sys.path; its presence and position depend on how Python is invoked (for example, it appears first when running interactively or with -c/-m, and is not added at all when running a script).
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 PYTHONPATH environment variable can be used to add custom directories to sys.path.
Why this is correct
PYTHONPATH is read by Python at start-up and its entries are prepended to sys.path, so custom directories become importable without editing code. This directly satisfies the stem's requirement that the variable can extend the module search path.
- ✗
The site-packages directory is searched before the PYTHONPATH directories.
Why it's wrong here
sys.path places PYTHONPATH entries ahead of the standard library and site-packages, so site-packages is searched after them, not before. The statement is tempting because site-packages does hold third-party modules, but ordering, not contents, is what this question tests.
- ✓
The directory containing the script being run is added to sys.path automatically.
Why this is correct
When you run a script, Python prepends that script's own directory to sys.path, so sibling modules import without installation. This automatic insertion is distinct from PYTHONPATH and site-packages handling, satisfying the stem's claim about the running script's directory.
- ✗
The current working directory is always the last entry in sys.path.
Why it's wrong here
The current working directory appears at the front of sys.path in interactive sessions and is absent entirely when running a script, so it is neither always present nor last. It is tempting because the working directory often does contribute to module resolution, but its position is not fixed.
- ✓
The sys.path can be modified at runtime to change the module search path.
Why this is correct
sys.path is an ordinary list, so appending, inserting or removing entries at runtime immediately alters module resolution for subsequent imports. This mutable behaviour satisfies the stem's statement that the search path can be changed while the interpreter runs.
Visual reference
Go deeper
Related to this question
About these practice questions
One of 421 original PCAP practice questions on Courseiva, each with a full explanation and wrong-answer analysis — not exam dumps or protected exam content. Learn why practice questions differ from exam dumps →
Same concept, more angles
1 more way this is tested on PCAP
These questions test the same concept from different angles. Work through them to make sure you can recognise it however the exam phrases it.
Variation 1. Which TWO of the following statements about Python's `sys.path` are true?
hard- A.The current working directory is always the first element in `sys.path`.
- ✓ B.Module search stops at the first matching directory in `sys.path`.
- C.`sys.path` is initialized from the PYTHONPATH environment variable.
- D.`sys.path` is a tuple of strings.
- ✓ E.The directory containing the script being run is added to the beginning of `sys.path` at startup.
Why B: Python's import mechanism iterates through `sys.path` in order and stops at the first directory containing the requested module. Option E is correct: the directory containing the script (or the current directory when running interactively) is inserted at the beginning of `sys.path` at startup. Options A, C, and D are false: the current working directory is not always first (the script's directory takes precedence), `sys.path` is initialized from the `PYTHONPATH` environment variable *in addition to* default paths, and `sys.path` is a list, not a tuple. Therefore, only two statements are true.
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.