Courseiva
Modules and PackageshardMultiple SelectObjective-mapped

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.

The PYTHONPATH environment variable is a standard mechanism for extending Python's module search path. When Python starts, it reads the PYTHONPATH variable and prepends its contents to sys.path, allowing users to specify additional directories where Python should look for modules before falling back to the default search order.

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 at startup and its entries are added to sys.path.

  • The site-packages directory is searched before the PYTHONPATH directories.

    Why it's wrong here

    PYTHONPATH entries are inserted before site-packages.

  • The directory containing the script being run is added to sys.path automatically.

    Why this is correct

    When a script is executed, its directory is added first.

  • The current working directory is always the last entry in sys.path.

    Why it's wrong here

    The current working directory is not automatically added; the script directory is.

  • The sys.path can be modified at runtime to change the module search path.

    Why this is correct

    It's a list; can be appended or inserted.

About these practice questions

One of 169 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 →

How Courseiva writes practice questions · Editorial policy

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.