Courseiva
Modules and PackageshardMultiple SelectObjective-mapped

Factors That Influence Python's Module Search Path (sys.path)

Which THREE factors influence Python's module search path (sys.path)?

Quick Answer

The answer is the PYTHONPATH environment variable, the directory containing the script being executed, and the site-packages directory. These three factors directly influence Python’s module search path, stored in sys.path, because the interpreter builds this list at startup by combining the directory of the input script, any paths specified in the PYTHONPATH variable, and the default site-packages directory where pip installs third-party libraries. On the Certified Associate Python Programmer PCAP exam, this question tests your understanding of how Python locates modules during import, often appearing as a multiple-select item. A common trap is forgetting that the current working directory is not automatically included—only the script’s directory is—or assuming that PYTHONPATH overrides everything, when it simply prepends to sys.path. To remember the three factors, think of the mnemonic “Script, Variable, Site”: the script’s directory, the PYTHONPATH variable, and the site-packages folder.

⚠ Common exam trap

Python Institute often tests the distinction between the current working directory at runtime and the directory containing the script being executed, leading candidates to incorrectly assume the working directory is always searched for modules.

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 site-packages directory where pip installs packages

The site-packages directory is automatically included in sys.path by the site module during Python's initialization. This directory is the default location where pip installs third-party packages, making them importable without manual path manipulation.

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 HOME environment variable

    Why it's wrong here

    HOME is not directly used for sys.path.

  • The current working directory at runtime

    Why it's wrong here

    Not added automatically; only the directory of the script is added.

  • The site-packages directory where pip installs packages

    Why this is correct

    Appended when site module is processed.

  • The directory containing the script being executed

    Why this is correct

    Added to sys.path[0] for the main script.

  • The PYTHONPATH environment variable

    Why this is correct

    Preprended to sys.path.

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

Same concept, more angles

2 more ways 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. A script runs: import sys; print(sys.path[0]). The output is an empty string. What does this indicate?

hard
  • A.The script is being read from stdin.
  • B.Python was launched with the -I flag.
  • C.The current working directory is not in sys.path.
  • D.The script is running from an interactive shell.

Why A: 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.

Variation 2. A Python script fails with 'ModuleNotFoundError: No module named 'myapp.config''. The environment variable PYTHONPATH is not set. Which of the following is the most likely cause?

medium
  • A.The module is located in a directory not included in sys.path
  • B.The module's __init__.py is missing
  • C.The module is installed in a different Python version's site-packages
  • D.The module has a syntax error

Why A: When PYTHONPATH is not set, Python relies solely on sys.path to locate modules. sys.path includes the script's directory, standard library paths, and site-packages. If 'myapp.config' is not in any of these directories, Python raises ModuleNotFoundError. Option A correctly identifies that the module is in a directory not included in sys.path.

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.