Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional import in Python when creating an object inheriting from it

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):
    ...
like image 326
Piotr Migdal Avatar asked Dec 28 '18 12:12

Piotr Migdal


People also ask

How do you import a condition in Python?

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.

What is import* in Python?

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.

What is import in Python Script?

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.


1 Answers

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:

  1. The user knows what to expect. import my_lib.keras is very likely to depend on keras
  2. Import errors happen during import, not during runtime
  3. You avoid a lot of potential issues by not relying on tricks to inherit from unimported classes
  4. The enduser can very easily switch between implementations by just changing 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
like image 134
Nils Werner Avatar answered Sep 28 '22 10:09

Nils Werner