How can I lazily import a module in python?
I have classes which require dependencies in order to be instantiated but are otherwise optional. I'd like to lazily import the dependencies and fail to instantiate the class if they aren't available. Note that these dependencies are not required at the package level (otherwise they'd be mandatory via setuptools). I currently have something like this: class Foo: def __init__(self): try: import module except ImportError: raise ModuleNotFoundError("...") def foo(self): import module Because this try/except pattern is common, I'd like to abstract it into a lazy importer. Ideally if module is avai
0 upvotes
View original discussion ↗
Comments
Sign in or sign up to comment and upvote.
No comments yet.