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):
...
keras
, no? Anyone who'dimport
your class would presumably want to use it, which means they depend on your class which depends onkeras
. I'm unclear what the point would be of importing your class without also necessarily importing its dependencies. – Finniemymodule.keras
andmymodule.tensorflow
, and importing one of those will immediately import its dependencies. – HinayanaPlotLossesKeras
andPlotLossesTFKeras
.) – Overelaborate