Search⌘ K
AI Features

Optional and Local Imports

Explore advanced import techniques in Python by understanding how to implement optional imports for fallback modules and local imports to limit scope. This lesson helps you improve your code's organization and efficiency by using imports strategically within your programs.

Optional imports

Optional imports are used when we have a preferred module or package that we want to use, but we also want a fallback in case it doesn’t exist. For example, we might use optional imports to support multiple versions of software or for speed ups.

Examples of optional import

Here’s an example from the package github2 that demonstrates how ...