Courseiva
Modules and PackageshardMultiple SelectObjective-mapped

PCAP sys.path Practice Question

Which TWO of the following statements about Python's `sys.path` are true?

⚠ Common exam trap

The Python Institute often tests that `sys.path` is a list, not a tuple, and that the script's directory, not the current working directory, is inserted first. Candidates may mistakenly think `PYTHONPATH` is the sole source of `sys.path` initialization, but it is only one of several sources.

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

Module search stops at the first matching directory in `sys.path`.

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.

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 current working directory is always the first element in `sys.path`.

    Why it's wrong here

    This is false because the first element of `sys.path` depends on how Python was started. When running a script, `sys.path[0]` is the script's directory, not necessarily the current working directory—the process's CWD could be different if the script was invoked with a path from elsewhere. Only in interactive mode or when executing `-c` does the empty string (representing the CWD) appear first, so saying "always" is incorrect.

  • Module search stops at the first matching directory in `sys.path`.

    Why this is correct

    This is true: the import system walks through the directories and zip archives listed in `sys.path` sequentially, and the first entry that contains the requested module (or package) is used; Python does not continue searching later entries for an alternative. This is why the order of `sys.path` is critical—adding a directory to the front can shadow a standard-library module or another installed package. If no matching module is found, an `ImportError` is raised after the entire list has been exhausted.

  • `sys.path` is initialized from the PYTHONPATH environment variable.

    Why it's wrong here

    This is false because `sys.path` is not built exclusively from the `PYTHONPATH` variable. At interpreter startup, Python constructs `sys.path` by combining (in order) the script's directory or the current working directory for interactive mode, the directories listed in `PYTHONPATH`, and installation-dependent default paths such as the standard library and `site-packages`. So `PYTHONPATH` is merely one contributing source, not the initializer of the entire list.

  • `sys.path` is a tuple of strings.

    Why it's wrong here

    This is false because `sys.path` is a built-in list, not a tuple. Unlike a tuple, a list is mutable, which allows programs to alter the import search path at runtime—for example, by calling `sys.path.append('/custom/path')` or inserting entries with `sys.path.insert(0, ...)`. This mutability is important for dynamic imports, but it also means the path list can be accidentally modified, affecting future imports.

  • The directory containing the script being run is added to the beginning of `sys.path` at startup.

    Why this is correct

    This is true: when a Python script is executed, Python inserts the absolute path of the directory containing that script at the very beginning of `sys.path`, i.e., at index 0. This ensures that local modules in the script's own directory take precedence over installed modules with the same name. For an interactive session or `-c` command, the first entry is instead an empty string, which represents the current working directory.

About these practice questions

This PCAP question is part of Courseiva's 169-question bank — original exam-style content with full explanations and wrong-answer analysis, never real exam questions or exam dumps. Learn why practice questions differ from exam dumps →

How Courseiva writes practice questions · Editorial policy

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.