Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error handling when importing modules

This probably has an obvious answer, but I'm a beginner. I've got a "module" (really just a file with a bunch of functions I often use) at the beginning of which I import a number of other modules. Because I work on many systems, however, not all modules may be able to load on any particular machine. To make things slightly more difficult, I also change the names of the packages when I import them -- for example, matplotlib gets abbreviated to mp.

What I'd like to do is only load those modules that exist on the system I'm currently using, and do some error handling on the ones that don't. The only way I can think of doing so is by enclosing each import statement inside its own try block, which seems pretty un-pythonic. If I enclose them all in the same try block, whichever module throws an error will prevent the subsequent modules from being loaded. Any ideas that might make things look prettier? It would be so easy if I didn't want to change their names...

like image 326
Dave Schultz Avatar asked Jun 28 '10 09:06

Dave Schultz


People also ask

What error is caused by importing an unknown module?

The ImportError is raised when an import statement has trouble successfully importing the specified module. Typically, such a problem is due to an invalid or incorrect path, which will raise a ModuleNotFoundError in Python 3.6 and newer versions.

What is meant by import error in Python?

ImportError is raised when a module, or member of a module, cannot be imported. There are a two conditions where an ImportError might be raised. If a module does not exist.

What happens when a Python module is imported?

When a module is first imported, Python searches for the module and if found, it creates a module object 1, initializing it. If the named module cannot be found, a ModuleNotFoundError is raised. Python implements various strategies to search for the named module when the import machinery is invoked.


1 Answers

I don't think try except block is un-pythonic; instead it's a common way to handle import on Python.

Quoting Dive into Python:

There are a lot of other uses for exceptions besides handling actual error conditions. A common use in the standard Python library is to try to import a module, and then check whether it worked. Importing a module that does not exist will raise an ImportError exception. You can use this to define multiple levels of functionality based on which modules are available at run-time, or to support multiple platforms (where platform-specific code is separated into different modules).

The next example demonstrates how to use an exception to support platform-specific functionality.

try:     import termios, TERMIOS                      except ImportError:     try:         import msvcrt                                except ImportError:         try:             from EasyDialogs import AskPassword          except ImportError:             getpass = default_getpass                    else:                                                getpass = AskPassword     else:         getpass = win_getpass else:     getpass = unix_getpass 
like image 86
systempuntoout Avatar answered Sep 28 '22 19:09

systempuntoout