Module Import Checking

Let's talk about how importlib is useful in module import checking.

Why do we need importlib?

Python has a coding style that is known as EAFP: Easier to ask for forgiveness than permission. What this means is that it’s often easier to just assume that something exists (like a key in a dict) and catch an exception if we’re wrong. We saw this previously where we would attempt to import a module and we caught the ImportError if it didn’t exist. What if we wanted to check and see if a module could be imported rather than just guessing? We can do that with importlib!

Let’s get started with the implementation

Let’s take a look:

Get hands-on with 1200+ tech skills courses.