Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
What happens behind execution of an import stat...
Search
Shivashis Padhi
August 16, 2020
Education
1.6k
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
550
Demystifying Web Map Servers
plant99
0
110
Other Decks in Education
See All in Education
吉祥寺.pmは1つじゃない — 複数イベント並走運営の12年 —
magnolia
0
1.3k
View Manipulation and Reduction - Lecture 9 - Information Visualisation (4019538FNR)
signer
PRO
1
2.7k
[2026前期火5] 論理学(京都大学文学部 前期 第9回)「正規化の停止性——ヒドラゲームによる証明」
yatabe
0
150
生成AI時代のエンジニア育成について考えてみた
akasan
0
160
Interaction - Lecture 10 - Information Visualisation (4019538FNR)
signer
PRO
0
2.7k
Catecismo 26 #2 - Do Credo; Introdução ao 1º artigo
cm_manaus
0
130
2026年度春学期 統計学 第9回 確からしさを記述する ー 確率 (2026. 5. 28)
akiraasano
PRO
0
120
AIには考えられないことを考えられる人になるために
iqbocchi
1
180
Public Space Is Not For Sale
drikkes
0
120
NDIAS Automotive / IoT CTF 2026 Recap - Keyfob & OSINT
himitu23
0
160
焦燥を平穏に変えるエンジニアのための哲学
ichimichi
5
4.5k
SL AMIGOS 教育格差と私たちの取り組み - スリランカの支援学校への支援プロジェクト:リシンドゥ リオ 氏 (別府溝部学園短期大学 ビジネス観光コース 留学生):2720 Japan O.K. ロータリーEクラブ2026年4月6日卓話
2720japanoke
0
640
Featured
See All Featured
Marketing to machines
jonoalderson
1
5.5k
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
620
The Pragmatic Product Professional
lauravandoore
37
7.3k
Optimizing for Happiness
mojombo
378
71k
SEOcharity - Dark patterns in SEO and UX: How to avoid them and build a more ethical web
sarafernandez
0
210
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
340
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2.3k
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
133
19k
Navigating Weather and Climate Data
rabernat
0
250
The Power of CSS Pseudo Elements
geoffreycrofte
82
6.3k
SEO Brein meetup: CTRL+C is not how to scale international SEO
lindahogenes
1
2.7k
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