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):
...
The Python import statement imports code from one module into another program. You can import all the code from a module by specifying the import keyword followed by the module you want to import.
In Python, you use the import keyword to make code in one module available in another. Imports in Python are important for structuring your code effectively. Using imports properly will make you more productive, allowing you to reuse code while keeping your projects maintainable.
Import in python is similar to #include header_file in C/C++. Python modules can get access to code from another module by importing the file/function using import. The import statement is the most common way of invoking the import machinery, but it is not the only way. import module_name.
The most straighforward and most easily understood solution would be to split your library into submodules.
It has several advantages over trying to do imports on object initialization:
import my_lib.keras
is very likely to depend on keras
import my_lib.keras
to import my_lib.tensorflow
Such a solution could look like
# mylib/__init__.py
class SomethingGeneric():
pass
def something_else():
pass
and then
# mylib/keras.py
import keras
class PlotLosses():
pass
and
# mylib/tensorflow.py
import tensorflow
class PlotLosses():
pass
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With