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
Sekolah Sabat - Triwulan 3 2026 - Pelajaran 12
hiola
0
11k
AIってなぁに?
kenichiota0711
0
830
[2026前期火5] 論理学(京都大学文学部 前期 第8回)「正規化定理の証明」
yatabe
0
340
One Year of Learning English with AI @エンジニア英語キャリア Meetup #3
kekekenta
0
230
チームの鏡になるー自分の癖を知ると、チームのパターンが見えてくる@スクフェス仙台
saorimurooka
0
180
AIがコードを書く時代に、何を教えるのか / What Should We Teach in the Age of AI-Generated Code?
ichiroc
1
560
[2026前期火5] 論理学(京都大学文学部 前期 第9回)「正規化の停止性——ヒドラゲームによる証明」
yatabe
0
290
人間のために、人間と共に働く時代の終わりの始まり
frievea
0
190
Examen de Selectividad. Geografía julio 2026 (Convocatoria Extraordinaria). UCLM
juanmartin2026
1
13k
Visionary Initiative: Future Intelligence 「未来の知性と社会の礎を築く」|Science Tokyo(東京科学大学)
sciencetokyo
PRO
0
1.8k
[2026前期火5] 論理学(京都大学文学部 前期 第11回)「ハーモニー:三層モデルと保存拡大」
yatabe
0
290
20260908_ターゲットとスコープを決める発表法
masakiokuda
1
110
Featured
See All Featured
Large-scale JavaScript Application Architecture
addyosmani
515
110k
GraphQLとの向き合い方2022年版
quramy
50
15k
Leadership Guide Workshop - DevTernity 2021
reverentgeek
1
370
Bridging the Design Gap: How Collaborative Modelling removes blockers to flow between stakeholders and teams @FastFlow conf
baasie
0
700
A designer walks into a library…
pauljervisheath
211
25k
世界の人気アプリ100個を分析して見えたペイウォール設計の心得
akihiro_kokubo
PRO
74
42k
A better future with KSS
kneath
240
18k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
35
2.8k
Product Roadmaps are Hard
iamctodd
55
13k
What does AI have to do with Human Rights?
axbom
PRO
1
2.4k
GraphQLの誤解/rethinking-graphql
sonatard
75
12k
Performance Is Good for Brains [We Love Speed 2024]
tammyeverts
12
1.8k
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