#------------------------------------------------------------------------------# # Filename: importer.py # # Created: 06/20/2015 # # Author: BoĊĦtjan Mejak # # E-mail: bostjan.xperia@gmail.com # # License: GPLv3 # #------------------------------------------------------------------------------# """ The importer module exposes a function named tryToImport() which takes two keyword arguments, module and namespace. The module argument must be passed with a module name as a string, and the namespace argument must always be passed with globals() as a value. If the import fails for any reason, an error message box is shown to the user. """ import importlib def tryToImport(module, namespace): """ Show a message box if a user-given module can't be imported. The function tryToImport() takes two arguments, module and namespace. When calling this function, the module argument must be passed as a string and the namespace argument must be passed with globals() as a value. """ try: moduleObject = importlib.import_module(module) namespace[module] = moduleObject except ImportError as module: import ctypes import sys PARENT = None MESSAGE = u"{importedModule} is missing.".format(importedModule=module.name) CAPTION = u"Application Error" STYLE = 0 | 16 # An error message box with an OK button ctypes.windll.user32.MessageBoxW(PARENT, MESSAGE, CAPTION, STYLE) sys.exit("Not all library dependencies were met.")