Importing Without "import": Behind the Scenes of importlib
summary
Covers exact import machinery: modules are plain module objects cached in sys.modules, import calls run finders that return ModuleSpec objects and loaders that exec or load binaries, and binding to names is the keyword’s job not the import builtin. Demonstrates using sys.meta_path and path_hooks to register a custom zipimport finder, a GitHub downloader, and even a hook that runs cargo to build Rust at import time and successfully load the compiled extension, while warning this is slow and unsafe for production and reloads need careful cache invalidation.
AI-written summary · about
chapters (10)
- 0:00Introduction
- 0:32Understanding modules
- 4:26The import process
- 10:03Namespace binding
- 11:38Search paths and hooks
- 14:43Basic import examples
- 16:16Zip file imports
- 17:09GitHub repository imports
- 20:43Rust and dynamic building
- 22:43Q&A and conclusion
related talks
26mWhat happens behind execution of an `import` statement?
45mWhat happens when you import a module?
28mImport Deep Dive
23mHooking into the import system
21mHacking the Python 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
