Python Interview > How does a python module search path work? > Flashcards
general summary
In Python, module imports are guided by a set of directories called the module search path.
Module Search Path
Default Values
Global: Configured at Python installation time.
User-specific: User’s home directory, described in the PYTHONHOME environment variable.
Module Search Path
Stack-Up Mechanism
Command Line: Specified using the -m or -c flags.
Environment: Dictated by PYTHONPATH.
Standard Defaults: Follows a predetermined sequence.
Module Search Path
Adjusting the Search Path
Manual Extension: Use sys.path to add locations programmatically.
User-Centered: Touched upon in the site module, useful for local modifications.
Code Example: Customizing the Module Search Path
import sys
Display the current module search path
print(sys.path)
Add a temporary directory to the module search path
sys.path.append('/path/to/custom/modules')