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
共感から、つくる: 変わり続ける自分と、誰かのための創造
micknerd
1
380
The Art & Science of Elearning
tmiket
1
220
教育現場から見た Ruby on Rails
yasslab
PRO
0
170
BITCOIN : Les fondamentaux !
rlifchitz
0
160
プログラミング言語において文字列を複数行にわたって だらだらと記載するアレ
sapi_kawahara
0
160
Course Review - Lecture 13 - Next Generation User Interfaces (4018166FNR)
signer
PRO
0
2.3k
JAWS-UG初心者支部#81 GWにEduJAWSと何か作ろうもくもく会!
otsuki
0
130
Gesture-based Interaction - Lecture 6 - Next Generation User Interfaces (4018166FNR)
signer
PRO
1
2.3k
Padlet opetuksessa
matleenalaakso
12
15k
Data Physicalisation - Lecture 9 - Next Generation User Interfaces (4018166FNR)
signer
PRO
1
1k
モブ社員がモブエンジニアを名乗って得られたこと_20260413
masakiokuda
4
520
Populism, Post-Liberalism & Climate Change
vyadav
0
180
Featured
See All Featured
The Cost Of JavaScript in 2023
addyosmani
55
10k
Ruling the World: When Life Gets Gamed
codingconduct
0
250
Stop Working from a Prison Cell
hatefulcrawdad
274
21k
Lightning Talk: Beautiful Slides for Beginners
inesmontani
PRO
2
570
How GitHub (no longer) Works
holman
316
150k
Build your cross-platform service in a week with App Engine
jlugia
234
18k
[RailsConf 2023] Rails as a piece of cake
palkan
59
6.7k
Build The Right Thing And Hit Your Dates
maggiecrowley
39
3.2k
Neural Spatial Audio Processing for Sound Field Analysis and Control
skoyamalab
0
320
Leading Effective Engineering Teams in the AI Era
addyosmani
9
2k
The Curse of the Amulet
leimatthew05
1
13k
Everyday Curiosity
cassininazir
0
220
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