What happens behind execution of an `import` statement?
summary
Explains the step‑by‑step mechanics: a sys.modules check short‑circuits if present, otherwise the importer walks sys.meta_path calling find_spec until a finder returns a ModuleSpec containing a loader, then the loader runs create_module/exec_module and installs the module; missing specs raise ModuleNotFoundError, runtime load failures raise ImportError. Uses importlib base classes and implements a meta_path finder plus a loader that fetches .py files over HTTP with basic auth; the demo imports testproject.2.3 from a file server and executes its function successfully.
AI-written summary · about
chapters (18)
- 0:00Start
- 3:31Regular packages
- 4:07Namespace packages - example
- 4:31Types of packages supported by default
- 5:03sys.modules
- 6:12Common use-cases of importlib
- 8:05Finders - examples
- 8:26Finders - function
- 9:09find_spec
- 9:32importing spam.ham
- 11:09an example of sys.meta_path and sys.path_hooks
- 11:27What are import hooks?
- 12:54Tasks of loaders
- 13:09example - loading
- 15:24chronological order
- 22:43What else can you do with import hooks?
- 23:13How have people used import hooks?
- 24:26sys.meta_path with virtual environment enabled
related talks
45mWhat happens when you import a module?
30mImporting Without "import": Behind the Scenes of importlib
21mHacking the Python Import System
28mImport Deep Dive
23mHooking into the import system
29m"from where?!? import what?!?"
26mModules 101: how to avoid spaghetti, big balls of mud and houses of straw!
32mfrom hat import rabbit: Hacking your way through Python's import machinery for fun and profit
