Bird
Raised Fist0
Pythonprogramming~10 mins

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

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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.

Practice

(1/5)
1. What does sys.path represent in Python?
easy
A. A list of directories where Python looks for modules to import
B. A list of installed Python packages
C. The current working directory only
D. The list of functions in a module

Solution

  1. Step 1: Understand the role of sys.path

    sys.path is a list that Python uses to find modules when you use import.
  2. Step 2: Identify what sys.path contains

    It contains folder paths where Python searches for modules, not packages or functions.
  3. Final Answer:

    A list of directories where Python looks for modules to import -> Option A
  4. Quick Check:

    sys.path = list of module search directories [OK]
Hint: Remember sys.path lists folders Python searches for modules [OK]
Common Mistakes:
  • Confusing sys.path with installed packages
  • Thinking sys.path is only the current folder
  • Assuming sys.path lists functions or classes
2. Which of the following is the correct way to add a new directory to Python's module search path at runtime?
easy
A. sys.path.append('/my/new/path')
B. sys.add_path('/my/new/path')
C. sys.path.add('/my/new/path')
D. sys.insert_path('/my/new/path')

Solution

  1. Step 1: Recall how to modify lists in Python

    sys.path is a list, so to add an item, we use append().
  2. Step 2: Check the method names

    Only append() is a valid list method; others like add_path or insert_path do not exist.
  3. Final Answer:

    sys.path.append('/my/new/path') -> Option A
  4. Quick Check:

    Use append() to add path to sys.path [OK]
Hint: Use list append() to add paths to sys.path [OK]
Common Mistakes:
  • Using non-existent sys methods like add_path
  • Trying to assign sys.path directly without list methods
  • Confusing append() with add() which lists don't have
3. Given this code snippet:
import sys
print(sys.path[0])

What will sys.path[0] usually contain when running a script?
medium
A. The Python installation directory
B. An empty string
C. The user's home directory
D. The directory of the script being run

Solution

  1. Step 1: Understand sys.path[0] meaning

    The first item in sys.path is usually the directory of the script being executed.
  2. Step 2: Confirm what sys.path[0] holds

    This allows Python to find modules in the same folder as the script.
  3. Final Answer:

    The directory of the script being run -> Option D
  4. Quick Check:

    sys.path[0] = script folder [OK]
Hint: sys.path[0] is script's folder path [OK]
Common Mistakes:
  • Thinking sys.path[0] is Python install folder
  • Assuming sys.path[0] is always empty string
  • Confusing with user's home directory
4. What is wrong with this code if it raises an ImportError?
import sys
sys.path = '/my/custom/path'
import mymodule
medium
A. The import statement must come before modifying sys.path
B. You cannot modify sys.path at runtime
C. sys.path should be a list, not a string
D. sys.path must be cleared before adding new paths

Solution

  1. Step 1: Check the type of sys.path

    sys.path must be a list of strings, but here it is assigned a single string.
  2. Step 2: Understand the error cause

    Assigning a string breaks the list structure, so Python cannot find modules properly, causing ImportError.
  3. Final Answer:

    sys.path should be a list, not a string -> Option C
  4. Quick Check:

    sys.path must be list, not string [OK]
Hint: sys.path must always be a list of paths [OK]
Common Mistakes:
  • Assigning a string instead of list to sys.path
  • Thinking import order always matters here
  • Believing sys.path cannot be changed at runtime
5. You want to temporarily add a folder /home/user/myproject/libs to the module search path only for the current script run. Which code snippet correctly does this without affecting other scripts?
hard
A. import sys sys.path = ['/home/user/myproject/libs']
B. import sys sys.path.insert(0, '/home/user/myproject/libs')
C. import sys sys.path.append('/home/user/myproject/libs') sys.path.clear()
D. import sys sys.path.remove('/home/user/myproject/libs')

Solution

  1. Step 1: Understand how to add a path temporarily

    Using sys.path.insert(0, ...) adds the folder at the front for this run only.
  2. Step 2: Check other options for correctness

    import sys sys.path = ['/home/user/myproject/libs'] replaces sys.path entirely, affecting all paths. import sys sys.path.append('/home/user/myproject/libs') sys.path.clear() clears sys.path after appending, removing all paths. import sys sys.path.remove('/home/user/myproject/libs') tries to remove a path not yet added.
  3. Final Answer:

    import sys sys.path.insert(0, '/home/user/myproject/libs') -> Option B
  4. Quick Check:

    Use sys.path.insert(0, path) to add temporarily [OK]
Hint: Insert path at front with sys.path.insert(0, path) [OK]
Common Mistakes:
  • Replacing sys.path instead of inserting
  • Clearing sys.path accidentally after append
  • Removing paths before adding them