我们从Python开源项目中,提取了以下6个代码示例,用于说明如何使用django.utils.module_loading.import_module()。
def get_apps(self): """ Get the list of installed apps and return the apps that have an emails module """ templates = [] for app in settings.INSTALLED_APPS: try: app = import_module(app + '.emails') templates += self.get_plugs_mail_classes(app) except ImportError: pass return templates
def get_channel_routings(): channel_routing = [] for app in settings.INSTALLED_APPS: try: routing = import_module("{}.routing".format(app)) channel_routing.extend(routing.channel_routing) except: continue return channel_routing
def get_class_from_path(path): # This function is helpful to avoid circular imports. module_name, class_name = path.rsplit(".", 1) class_ = getattr(import_module(module_name), class_name) return class_
def import_app_module(app_module_name, module_name): full_module_name = '%s.%s' % (app_module_name, module_name) try: import_module(full_module_name) except ImportError: # we need to re-raise exception in case there was import error inside # `module_name` module module_file_name = get_module_file_name(app_module_name, module_name) if os.path.exists(module_file_name): raise
def get_module_file_name(app_module_name, module_name): module = import_module(app_module_name) module_dir = os.path.dirname(module.__file__) return os.path.join(module_dir, '%s.py' % module_name)
def autodiscover(): for app in settings.INSTALLED_APPS: mod = import_module(app) try: import_module('%s.%s' % (app, 'link_resolvers')) except: if module_has_submodule(mod, 'link_resolvers'): raise