I have a Python program that has several slow imports. I’d like to delay importing them until they are needed. For instance, if a user is just trying to print a help message, it is silly to import the slow modules. What’s the most Pythonic way to do this?
I’ll add a solution I was playing with as a answer. I know you all can do better, though.
Just import them where they’re needed. After a module has been imported once, it is cached so that any subsequent imports will be quick. If you import the same module 20 times, only the first one will be slow.