Bird
0
0

Why does Python search the directories in sys.path in order when importing a module?

hard📝 Conceptual Q10 of 15
Python - Modules and Code Organization
Why does Python search the directories in sys.path in order when importing a module?
ATo check for syntax errors in each directory.
BTo load all modules with the same name from every directory.
CTo find the first matching module and avoid loading duplicates.
DBecause sys.path is sorted alphabetically by default.
Step-by-Step Solution
Solution:
  1. Step 1: Understand module import order

    Python searches sys.path in order to find the first module matching the import name.
  2. Step 2: Avoid loading duplicates

    Once found, Python stops searching to prevent loading multiple modules with the same name.
  3. Final Answer:

    To find the first matching module and avoid loading duplicates. -> Option C
  4. Quick Check:

    Ordered search stops at first match = D [OK]
Quick Trick: Python stops searching sys.path after first module found [OK]
Common Mistakes:
  • Thinking Python loads all modules with same name
  • Assuming sys.path is alphabetically sorted
  • Believing Python checks syntax in all directories

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Python Quizzes