Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Sign up for free
Menu
Search
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Features
All features
Private URLs
Password Protection
Custom URLS
Scheduled publishing
Remove Branding
Restrict embedding
Deck Collections
Notes
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Explore
Featured decks
Featured speakers
Programming
Technology
Storyboards
Pricing
Search
Sign in
Sign up for free
What happens behind execution of an import stat...
Search
Shivashis Padhi
August 16, 2020
Education
1.7k
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
What happens behind execution of an import statement?
Shivashis Padhi
August 16, 2020
More Decks by Shivashis Padhi
See All by Shivashis Padhi
WIP: What happens behind execution of an `import` statement?
plant99
0
560
Demystifying Web Map Servers
plant99
0
110
Other Decks in Education
See All in Education
AI時代のセキュリティ監査
matshogo
PRO
0
110
遊ぶかね欲しさの犯行(ルビ:労働)です
shirayanagiryuji
0
260
Throw Yourself In! - How I've learned English and What I'm Facing
georgeorge
1
260
SFプロトタイピング体験(滝中学校)
yusk1450
PRO
0
130
学生のうちに考えておきたい信頼の話
suisan
1
770
Plano urbano de Madrid desde el Ensanche al s. XXI (2ª parte).
juanmartin2026
1
59k
Comentario del plano urbano de Madrid hasta 1860 (1ª parte )
juanmartin2026
1
59k
子どものためのプログラミング道場『CoderDojo』〜法人提携例〜 / Partnership with CoderDojo Japan
coderdojojapan
PRO
4
19k
2026年度春学期 統計学 第10回 分布の推測とは - 標本調査,度数分布と確率分布 (2026. 6. 4)
akiraasano
PRO
0
210
Leveraging LLMs for student feedback in introductory data science courses
minecr
0
110
2026年度春学期 統計学 第15回 分布についての仮説を検証する ― 仮説検定(2) (2026. 7. 9)
akiraasano
PRO
0
130
Visionary Initiative: Future Intelligence — Laying the foundations for the future of science, intelligence, and society | Science Tokyo
sciencetokyo
PRO
0
290
Featured
See All Featured
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
35
3.8k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Build your cross-platform service in a week with App Engine
jlugia
234
19k
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2.6k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
232
55k
Future Trends and Review - Lecture 12 - Web Technologies (1019888BNR)
signer
PRO
0
3.7k
Making the Leap to Tech Lead
cromwellryan
135
10k
The browser strikes back
jonoalderson
0
1.6k
Balancing Empowerment & Direction
lara
6
1.3k
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
9.2k
Transcript
What happens behind execution of an import statement? - Shivashis
Padhi
Who am I? - data/software at Grofers - tinker with
earth observation data - past GSoC with the PSF - love FOSS. worked on felicette, MSS
None
xkcd/353
Packages in Python
Packages ‣ directories? not necessarily ‣ special types of modules
‣ the __path__ attribute ‣ What are subpackages?
None
But how can we import a package from a different
directory?
$PATH, and $PYTHONPATH
None
Regular and Namespace packages
Regular packages
Namespace packages
Namespace packages - example
Types of packages supported by default ‣ built-in modules ‣
frozen modules ‣ path entries in $PATH, and $PYTHONPATH
The import system
sys.modules ‣ A dictionary to map module names to modules
which are ‘already loaded’. ‣ Lives in the global namespace of sys
sys.modules
importlib ‣ provide the implementation of the import statement ‣
expose parts of the import system for programmatic usage
Common use-cases of importlib ‣ Programmatically loading a module ‣
Reloading a module in run-time ‣ Base classes for finders and loaders (to be used with import hooks)
Finders and loaders
Finders ‣ find the module ‣ return a spec object
‣ Finder object must have a find_spec method defined.
Finders - examples ‣ locate built-in modules - BuiltinImporter ‣
locate frozen modules - FrozenImporter ‣ import path - imagine $PATH and $PYTHONPATH - PathFinder
Finders - function ‣ If a finder is capable of
handling import, it returns a module spec. ‣ module spec contains module’s metadata i.e path, name, etc. The import machinery uses this spec to load modules later.
find_spec
importing spam.ham
Import hooks and PEP 302
sys.path_hooks sys.meta_path
an example of sys.meta_path and sys.path_hooks
What are import hooks? ‣ Inserting custom finders and loaders
into the import mechanism ‣ Dependency injection?
Loaders After spec object is returned from a finder, loaders
are used to execute the module and store its reference in sys.modules
Tasks of loaders ‣ load from source/byte-code ‣ compile source
‣ execute the module and store reference in sys.modules[spec.name]
example - loading
example - loading
example - loading
ImportError ModuleNotFoundError When no finder can find a module When
loading fails for a package/module
chronological order
Code demonstration of an import hooks’ implementation.
What else can you do with import hooks? ‣ full
control after what happens after import x ‣ importing from ftp, https, authorization checks. ‣ importing from a variety of storage, not just .py/.pyc
How have people used import hooks? ‣ https://github.com/kragniz/json-sempai ‣ https://github.com/nvbn/import_from_github_com
‣ https://github.com/runsascoded/ur
sys.meta_path with virtual environment disabled
sys.meta_path with virtual environment enabled Virtual Environment’s finder
Thank you! https://shivashis.xyz