diff options
| author | Claude Paroz <claude@2xlibre.net> | 2013-02-02 22:58:02 +0100 |
|---|---|---|
| committer | Claude Paroz <claude@2xlibre.net> | 2013-02-04 16:38:25 +0100 |
| commit | 7c5b244826be636429791a8ca76b2adc678e82e7 (patch) | |
| tree | 7a7f8d23cecd8c55f2f2f9c243376d036d20a0bb /django/utils/module_loading.py | |
| parent | 3f1c7b70537330435e2ec2fca9550f7b7fa4372e (diff) | |
Fixed #17061 -- Factored out importing object from a dotted path
Thanks Carl Meyer for the report.
Diffstat (limited to 'django/utils/module_loading.py')
| -rw-r--r-- | django/utils/module_loading.py | 26 |
1 files changed, 26 insertions, 0 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index f8aadb34df..01996743cd 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -2,6 +2,32 @@ import imp import os import sys +from django.core.exceptions import ImproperlyConfigured +from django.utils.importlib import import_module + + +def import_by_path(dotted_path, error_prefix=''): + """ + Import a dotted module path and return the attribute/class designated by the + last name in the path. Raise ImproperlyConfigured if something goes wrong. + """ + try: + module_path, class_name = dotted_path.rsplit('.', 1) + except ValueError: + raise ImproperlyConfigured("%s%s doesn't look like a module path" % ( + error_prefix, dotted_path)) + try: + module = import_module(module_path) + except ImportError as e: + raise ImproperlyConfigured('%sError importing module %s: "%s"' % ( + error_prefix, module_path, e)) + try: + attr = getattr(module, class_name) + except AttributeError: + raise ImproperlyConfigured('%sModule "%s" does not define a "%s" attribute/class' % ( + error_prefix, module_path, class_name)) + return attr + def module_has_submodule(package, module_name): """See if 'module' is in 'package'.""" |
