0
0
Pythonprogramming~10 mins

Module search path in Python - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Module search path
Start: import module
Check built-in modules
No
Check current directory
No
Check directories in sys.path one by one
Load module
When Python imports a module, it looks in built-in modules, then current directory, then each folder in sys.path until it finds the module or raises an error.
Execution Sample
Python
import sys
print(sys.path)
This code prints the list of directories Python searches for modules.
Execution Table
StepActionModule Found?Directory CheckedResult
1Check built-in modulesNoN/AContinue searching
2Check current directoryNo./ (current folder)Continue searching
3Check sys.path[0]No/usr/lib/python3.12Continue searching
4Check sys.path[1]Yes/home/user/projectsModule loaded
5EndN/AN/AImport successful
💡 Module found in /home/user/projects, search stops.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
module_foundFalseFalseFalseFalseTrueTrue
directory_checkedNonebuilt-incurrent dir/usr/lib/python3.12/home/user/projects/home/user/projects
Key Moments - 2 Insights
Why does Python check the current directory before sys.path folders?
Python checks the current directory early to allow importing modules from your working folder easily, as shown in execution_table step 2.
What happens if the module is not found in any directory?
If no directory has the module, Python raises ModuleNotFoundError after checking all sys.path entries, as implied after step 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, at which step is the module found?
AStep 4
BStep 2
CStep 3
DStep 1
💡 Hint
Check the 'Module Found?' column in execution_table rows.
According to variable_tracker, what is the value of 'module_found' after step 3?
ATrue
BNone
CFalse
DError
💡 Hint
Look at the 'module_found' row under 'After Step 3' column in variable_tracker.
If the module was in the current directory, how would the execution_table change?
AModule Found? would be No at all steps
BModule Found? would be Yes at step 2
CModule Found? would be Yes at step 3
DModule Found? would be Yes at step 1
💡 Hint
Current directory is checked at step 2 in execution_table.
Concept Snapshot
Module search path in Python:
- Python looks for modules in this order:
  1. Built-in modules
  2. Current directory
  3. Directories in sys.path
- Stops at first found module
- If not found, raises ModuleNotFoundError
Full Transcript
When you import a module in Python, the interpreter searches for it in a specific order. First, it checks if the module is built-in. If not found, it looks in the current working directory. If still not found, it searches each folder listed in sys.path one by one. Once the module is found, Python loads it and stops searching. If the module is not found anywhere, Python raises a ModuleNotFoundError. This process helps Python know where to find the code you want to use.