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
0
1.6k
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
530
Demystifying Web Map Servers
plant99
0
100
Other Decks in Education
See All in Education
Security, Privacy and Trust - Lecture 11 - Web Technologies (1019888BNR)
signer
PRO
0
3.2k
Evaluation Methods - Lecture 6 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
1.2k
卒論の書き方 / Happy Writing
kaityo256
PRO
54
28k
1125
cbtlibrary
0
160
Going over the Edge
jonoalderson
0
280
LotusScript でエージェント情報を出力してみた
harunakano
0
110
HyRead2526
cbtlibrary
0
180
ThingLink
matleenalaakso
28
4.3k
Human Perception and Cognition - Lecture 4 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
1.3k
俺と地方勉強会 - KomeKaigi・地方勉強会への期待 -
pharaohkj
1
1.6k
Design Guidelines and Models - Lecture 5 - Human-Computer Interaction (1023841ANR)
signer
PRO
0
1.2k
3Dプリンタでロボット作るよ#5_ロボット向け3Dプリンタ材料
shiba_8ro
0
140
Featured
See All Featured
CoffeeScript is Beautiful & I Never Want to Write Plain JavaScript Again
sstephenson
162
16k
Digital Projects Gone Horribly Wrong (And the UX Pros Who Still Save the Day) - Dean Schuster
uxyall
0
120
Build your cross-platform service in a week with App Engine
jlugia
234
18k
Unlocking the hidden potential of vector embeddings in international SEO
frankvandijk
0
140
Learning to Love Humans: Emotional Interface Design
aarron
274
41k
YesSQL, Process and Tooling at Scale
rocio
174
15k
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
1
270
Applied NLP in the Age of Generative AI
inesmontani
PRO
4
2k
Utilizing Notion as your number one productivity tool
mfonobong
2
200
Designing for humans not robots
tammielis
254
26k
I Don’t Have Time: Getting Over the Fear to Launch Your Podcast
jcasabona
34
2.6k
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
12
1k
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