I create a package connecting to other libraries (livelossplot). It has a lot of optional dependencies (deep learning frameworks), and I don't want to force people to install them.
Right now I use conditional imports, in the spirit of:
try:
from .keras_plot import PlotLossesKeras
except ImportError:
# import keras plot only if there is keras
pass
However, it means that it imports big libraries, even if one does not intend to use them. The question is: how to import libraries only when one creates a particular object?
For Python functions, it is simple:
def function_using_keras():
import keras
...
What is a good practice for classes inheriting from other classes?
It seems that a parent class needs to be imported before defining an object:
from keras.callbacks import Callback
class PlotLossesKeras(Callback):
...