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
540
Demystifying Web Map Servers
plant99
0
110
Other Decks in Education
See All in Education
P3NFEST 2026 Spring ハンズオン「ハッキング・ラブ!はじめてのハッキングをやってみよう」資料
nomizone
0
360
Chapitre_2_-_Partie_2.pdf
bernhardsvt
2
230
What workforce agencies must have in place to compete for and deliver on RESTART grants
territorium
PRO
0
120
Going over the Edge
jonoalderson
0
790
Introduction - Lecture 1 - Next Generation User Interfaces (4018166FNR)
signer
PRO
2
4.5k
Sponsorship 2026 | VizChitra
vizchitra
1
120
SSH公開鍵認証 / 02-b-ssh
kaityo256
PRO
0
120
0203
cbtlibrary
0
150
この講義について / 00-setup
kaityo256
PRO
0
240
Measuring your measuring
jonoalderson
2
780
栃木にいても「だいじ」だっぺ〜! 栃木&全国アジャイルコミュニティへの参加・運営の魅力
sasakendayo
1
180
OSINT入門-CTF for GIRLS_SECCON14電脳会議
nomizone
1
1.2k
Featured
See All Featured
Dominate Local Search Results - an insider guide to GBP, reviews, and Local SEO
greggifford
PRO
0
110
Building an army of robots
kneath
306
46k
The Illustrated Guide to Node.js - THAT Conference 2024
reverentgeek
1
320
Bootstrapping a Software Product
garrettdimon
PRO
307
120k
Exploring the relationship between traditional SERPs and Gen AI search
raygrieselhuber
PRO
2
3.7k
Code Review Best Practice
trishagee
74
20k
The Art of Delivering Value - GDevCon NA Keynote
reverentgeek
16
1.9k
How to Think Like a Performance Engineer
csswizardry
28
2.5k
Are puppies a ranking factor?
jonoalderson
1
3.1k
[SF Ruby Conf 2025] Rails X
palkan
2
850
How Software Deployment tools have changed in the past 20 years
geshan
0
33k
brightonSEO & MeasureFest 2025 - Christian Goodrich - Winning strategies for Black Friday CRO & PPC
cargoodrich
3
130
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