What happens behind execution of an `import` statement?

Sat September 05, 11:00 AM–11:25 AM • Back to program
Start time 11:00
End time 11:25
Countdown link Open timer

Very often we in our daily usage of Python, we face errors related to $PATH, $PYTHONPATH, or other import related errors. A clear understanding of the import system helps us to fix these errors properly, and build extensions on top of the import system.

This talk follows this template to explain what goes on in the background when you execute from spam import ham, or any other import statement. Additionally, it discusses import hooks(PEP-302) and how to use them.

Introduction

Every Python script involves import statements. And if you develop software with Python, you might already have come across import and path related errors like ImportError, ModuleNotFoundError, etc.

It's important to know the import mechanics, both for general knowledge, and to develop custom plugins to tweak the import system. In this talk, we'd discuss in detail about how the import system works, its components and what tasks they perform respectively.

Who should attend this talk?

This talk is most suitable for beginner to intermediate level Pythonistas.

How does the talk proceed?

What can someone get out of this talk?

An understanding of the import system's mechanics, which helps to quickly debug and fix path/import related errors. Knowledge of import hooks, to build custom import related plugins.

Outline

Time Duration - Topic

0 - 4 Introduction to packages and how to organize packages

4 - 7 Types of packages in python, and the default ones the import system supports.

7 - 12 Components of import system - sys.modules - finders and loaders - chronology of import related tasks

12 - 15 Finders, examples of finders, and how they function, find_spec function

15 - 17 sys.meta_path, sys.path_hooks in detail

17 - 19 Introduction to PEP 302, and import hooks

19 - 22 explain loaders with template code

23 - 25 code walkthrough and demonstration of an implementation of import hooks

25 - 26 briefly cover 'importing of submodules' topic with an example

26 - 28 present use-cases of import hooks, and usage in industry, conclude.

28 - 30 Q&A

Prerequisites:

If you don't know either and still like the talk idea, please follow this gist to 'intentionally' come across the errors :)

Content URLs

WIP slides

Longer description of the talk idea as blogpost

Shivashis Padhi He/Him

Shivashis Padhi is a senior, majoring Computer Science and Engineering at National Institute of Technology, Tiruchirappalli, India.

With ~3 years of experience as a student software developer he has worked with multiple organizations(Python Software Foundation - GSoC'20, Delta Force), large and small scale startups(Grofers, Flytbase Labs, Gmetri), he lives by a simple policy, 'learn and build stuff to make the world a better place'. He's currently an intern with the Data Engineering team at Grofers building geospatial data-management tools. He spends some time contributing to MSS with an amazing set of people.

He also gave his first ever technical talk, about maps and Python during PyCon India'19.

Keeping work aside, he's a big fan of earth, computers, maps, classical music from different cultures, and history.