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
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
540
Demystifying Web Map Servers
plant99
0
110
Other Decks in Education
See All in Education
PE array testbench data order (config)
songchch
0
460
共感から、つくる: 変わり続ける自分と、誰かのための創造
micknerd
1
270
What workforce agencies must have in place to compete for and deliver on RESTART grants
territorium
PRO
0
130
Google AI Studio 開発者向け完全ガイド プロトタイピ まで ングからReactアプリ 構築・マルチモーダル活用
mickey_kubo
1
110
ブランチ操作 / 02-a-branch
kaityo256
PRO
0
240
Measuring what matters
jonoalderson
0
150
Postcards
gabrielramirezv
0
170
AI進化史:LLMからAIエージェントへ
mickey_kubo
0
100
Padlet opetuksessa
matleenalaakso
12
15k
PE array testbench data order (data)
songchch
0
470
Sponsorship 2026 | VizChitra
vizchitra
2
160
Highest and Best Use: Development Considerations for Land Sites
rmccaic
0
190
Featured
See All Featured
Skip the Path - Find Your Career Trail
mkilby
1
94
AI in Enterprises - Java and Open Source to the Rescue
ivargrimstad
0
1.2k
Hiding What from Whom? A Critical Review of the History of Programming languages for Music
tomoyanonymous
2
660
The SEO Collaboration Effect
kristinabergwall1
0
410
What the history of the web can teach us about the future of AI
inesmontani
PRO
1
500
The Pragmatic Product Professional
lauravandoore
37
7.2k
実際に使うSQLの書き方 徹底解説 / pgcon21j-tutorial
soudai
PRO
199
73k
Documentation Writing (for coders)
carmenintech
77
5.3k
エンジニアに許された特別な時間の終わり
watany
106
240k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
508
140k
Building AI with AI
inesmontani
PRO
1
860
Prompt Engineering for Job Search
mfonobong
0
250
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