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 statement?
Search
Shivashis Padhi
August 16, 2020
Education
0
1.3k
What happens behind execution of an import statement?
Shivashis Padhi
August 16, 2020
Tweet
Share
More Decks by Shivashis Padhi
See All by Shivashis Padhi
WIP: What happens behind execution of an `import` statement?
plant99
0
340
Demystifying Web Map Servers
plant99
0
93
Other Decks in Education
See All in Education
Contentless Marketing
jonoalderson
0
880
Цифровые финансы - магистерская программа Финэка МГИМО 2024 г.
niellony
0
400
いまからでも遅くない!サイバーセキュリティ人材育成
mnori0211
1
1.6k
2024年度春学期 統計学 第9回 確からしさを記述する ー 確率 (2024. 6. 6)
akiraasano
PRO
0
110
WaPEN_Asial2024.pdf
asial_edu
0
360
制限ボルツマンマシンの話 / Introduction of RBM
kaityo256
PRO
3
600
0521
cbtlibrary
0
110
week15@tcue2024
nonxxxizm
0
380
🎓 ChatGPT を組み込んだ24時間TA : 教育現場における LLM 活用の課題と改善
yasslab
PRO
0
600
小・中・高等学校における情報教育の体系的な学習を目指したカリキュラムモデル案/curriculum model
codeforeveryone
0
230
week4@tcue2024
nonxxxizm
0
520
大学院進学について(2024年度版)
imash
0
140
Featured
See All Featured
Designing Experiences People Love
moore
136
23k
Web Components: a chance to create the future
zenorocha
307
41k
Visualizing Your Data: Incorporating Mongo into Loggly Infrastructure
mongodb
36
9.1k
What the flash - Photography Introduction
edds
65
11k
Why You Should Never Use an ORM
jnunemaker
PRO
51
8.9k
Building Flexible Design Systems
yeseniaperezcruz
323
37k
How To Stay Up To Date on Web Technology
chriscoyier
784
250k
The Cult of Friendly URLs
andyhume
75
5.9k
Understanding Cognitive Biases in Performance Measurement
bluesmoon
18
1.2k
The Brand Is Dead. Long Live the Brand.
mthomps
52
36k
The Straight Up "How To Draw Better" Workshop
denniskardys
229
130k
Music & Morning Musume
bryan
43
5.9k
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