Q44: How does Python's Import System search for modules via sys.path?
When an import statement executes, Python searches locations listed in sys.path in order:
- Directory containing the input script.
- Directories listed in
PYTHONPATH. - Standard library directories.
- Installed third-party
site-packages.
Key Takeaways
- Search path sequence stored in
sys.pathlist. - Loaded modules are cached in
sys.modulesdictionary to prevent re-parsing.
Knowledge Check
Where does Python cache successfully imported module objects to prevent redundant disk reads?