Member-only story
What You Need To Know About Import System — Python
How It Works From Finding To Loading Your Module
Not a member yet? Read it for free here.
I never stopped to think about how the import system in Python or any other programming language works until recently when I was writing about circular import errors.
Like most things that can appear simple at a high level, once you dive into the subject, you’ll see there’s much more to discover.
Understanding how the Python import system works can improve your code’s performance and save you from unexpected problems like those mentioned above.
In Python, there are three ways of importing a module: 1) using the import statement, 2) using the importlib library, and 3) using the dunder method __import__.
Before we delve into the import system, we first need to understand how Python treats packages and modules.