r/Python • • Jan 05 '18

Really??

Post image
2.0k Upvotes

317 comments sorted by

View all comments

Show parent comments

5

u/[deleted] Jan 05 '18

Well, maybe one missing detail is that it doesn't really override the original import but just wraps it. The original import instruction is used internally to import all other non-optional modules and optional modules that are installed.

2

u/rhytnen Jan 05 '18

help me understand .... here are some scenarios. what is happening your infrastructure that requires something else?

from your_lib import required_libs
try:
    from your_lib import optional_lib
except ImportError:
    logging.warning('optional_lib not found, xyz not supported')

try:
    import some_lib
except ImportError:
    import some_other_lib as some_lib

1

u/[deleted] Jan 06 '18

Okay...

Yes, that's one way to do it, but...

What about the places where the modules are actually used? We would have had to catch those calls as well in the case that a module is not available. That were a huge amount of additional try-except blocks and another huge amount of actual error handling (avoiding all calls to that module). We tried that. It's ugly.

Specifically for matplotlib that hasn't made much sense to us. It's totally okay that the program runs with or without matplotlib since matplotlib does not alter anything. The MagicMock objects returned by the wrapped import do no harm. They just pretend to do what matplotlib was supposed to do - without actually doing it. A dry run, if you will. The user will be informed that matplotlib is optional and not installed so that they don't wonder why visualization does not work.

To help you understand: we were after a solution where most of the already existing code could remain untouched. Only specific modules are marked optional. That is, if a module is missing that is not explicitly marked optional, Python will still raise an ImportError. Otherwise it will only show a warning, but the program will continue to run. The thing is, the existing code base is quite large and quite old. We still have instructions/syntax in there that date back to Python 2.5. Messing with that code aside from upgrading it to at least Python 2.7 is a risky challenge.

Furthermore, it's political. There's currently no official project that involves working on the code. So everything that has to be done, must take as little time as possible.

Wrapping import was the quickest and cleanest solution we could come up with within the available time. It may not be beautiful but it works and we're okay with it. It's also documented so that future developers will understand what's happening.

2

u/rhytnen Jan 06 '18

I mean I do exactly that. Here's an excerpt from some code of mine.

try:
    import matplotlib.pyplot as plt
except ImportError:
    plt = None
    print( "Warning: could not import from matplotlib. Echogram.show() will not work.")

Now later on in the class that would call imshow ...

    def show(self):
    '''
    Plot an image of the echogram.
    '''
    if plt is None:
        logging.warning("matplotlib was not imported, cannot plot")
        return

    plt.imshow(self.data, aspect='auto')

So your code (apparently) ...

  • literally changes the semantic meaning of a python builtin

  • alters behavior the user may not be expecting

  • probably makes the code seem odd to a new comer

I would prefer my solution - which isn't full of thousands of try / excepts - any day.

→ More replies (0)