Upgrade to Pro — share decks privately, control downloads, hide ads and more …

WIP: What happens behind execution of an `import` statement?

Shivashis Padhi
May 02, 2020
260

WIP: What happens behind execution of an `import` statement?

Shivashis Padhi

May 02, 2020
Tweet

Transcript

  1. What happens when you do
    >> from spam import ham
    - Shivashis Padhi

    View full-size slide

  2. Packages in Python

    View full-size slide

  3. But how can we import a package from a different directory?

    View full-size slide

  4. $PATH, and $PYTHONPATH

    View full-size slide

  5. Regular and Namespace packages

    View full-size slide

  6. The import system

    View full-size slide

  7. Finders and loaders

    View full-size slide

  8. Finders - examples
    • locate built-in modules
    • locate frozen modules
    • import path - imagine $PATH and $PYTHONPATH

    View full-size slide

  9. 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

    View full-size slide

  10. importing spam.ham

    View full-size slide

  11. Import hooks and PEP 302

    View full-size slide

  12. sys.path_hooks
    sys.meta_path

    View full-size slide

  13. Loaders
    After spec object is returned from a finder,
    loaders are used to execute the module and
    store its reference in sys.modules

    View full-size slide

  14. Code demonstration of an
    import hooks’ implementation.

    View full-size slide

  15. importing submodules

    View full-size slide

  16. 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

    View full-size slide