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
Recall & Review
beginner
What is the module search path in Python?
The module search path is the list of directories Python looks through to find a module when you use import. It tells Python where to find the files you want to use.
Click to reveal answer
beginner
How can you see the current module search path in Python?
You can see it by importing the sys module and printing sys.path. This shows a list of folders Python checks for modules.
Click to reveal answer
beginner
What happens if Python cannot find a module in the search path?
Python raises a ModuleNotFoundError. This means it looked in all the folders in the search path but did not find the module file.
Click to reveal answer
intermediate
How can you add a new folder to the module search path temporarily?
You can add a folder by appending its path to sys.path in your script. This lets Python look in that folder for modules during that run.
Click to reveal answer
beginner
Why is the current working directory usually first in the module search path?
Because Python tries to import modules from your current folder first. This helps you use your own files before looking in system or library folders.
Click to reveal answer
What Python list shows the directories where modules are searched?
Asys.path
Bos.environ
Csys.modules
Dimport.path
✗ Incorrect
The sys.path list contains all directories Python checks for modules.
If a module is not found, what error does Python raise?
AFileNotFoundError
BModuleNotFoundError
CImportError
DNameError
✗ Incorrect
ModuleNotFoundError is raised when Python cannot find the module in the search path.
Which folder is usually checked first when importing a module?
ASystem library folder
BUser home directory
CCurrent working directory
DTemporary files folder
✗ Incorrect
Python checks the current working directory first to find modules.
How can you add a folder to the module search path during a script run?
AAppend the folder path to sys.path
BSet PYTHONPATH environment variable inside the script
CUse importlib.add_path()
DCreate a new module named sys.path
✗ Incorrect
Appending a folder path to sys.path adds it temporarily to the search path.
What does the sys.path list include?
AEnvironment variables
BList of all imported modules
CPython version information
DDirectories where Python looks for modules
✗ Incorrect
sys.path is a list of directories Python searches for modules.
Explain what the Python module search path is and why it matters when importing modules.
Think about how Python finds the files you want to use.
You got /3 concepts.
Describe how you can check and modify the module search path in a Python script.
Use the sys module to see and change the search path.
You got /3 concepts.
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
Step 1: Understand the role of sys.path
sys.path is a list that Python uses to find modules when you use import.
Step 2: Identify what sys.path contains
It contains folder paths where Python searches for modules, not packages or functions.
Final Answer:
A list of directories where Python looks for modules to import -> Option A
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
Step 1: Recall how to modify lists in Python
sys.path is a list, so to add an item, we use append().
Step 2: Check the method names
Only append() is a valid list method; others like add_path or insert_path do not exist.
Final Answer:
sys.path.append('/my/new/path') -> Option A
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
Step 1: Understand sys.path[0] meaning
The first item in sys.path is usually the directory of the script being executed.
Step 2: Confirm what sys.path[0] holds
This allows Python to find modules in the same folder as the script.
Final Answer:
The directory of the script being run -> Option D
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?
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
Step 1: Check the type of sys.path
sys.path must be a list of strings, but here it is assigned a single string.
Step 2: Understand the error cause
Assigning a string breaks the list structure, so Python cannot find modules properly, causing ImportError.
Final Answer:
sys.path should be a list, not a string -> Option C
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
Step 1: Understand how to add a path temporarily
Using sys.path.insert(0, ...) adds the folder at the front for this run only.
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.
Final Answer:
import sys
sys.path.insert(0, '/home/user/myproject/libs') -> Option B
Quick Check:
Use sys.path.insert(0, path) to add temporarily [OK]
Hint: Insert path at front with sys.path.insert(0, path) [OK]