diff options
Diffstat (limited to 'django/utils/module_loading.py')
| -rw-r--r-- | django/utils/module_loading.py | 18 |
1 files changed, 18 insertions, 0 deletions
diff --git a/django/utils/module_loading.py b/django/utils/module_loading.py index 55d89caa46..5dc3aefe75 100644 --- a/django/utils/module_loading.py +++ b/django/utils/module_loading.py @@ -148,3 +148,21 @@ else: else: # Exhausted the search, so the module cannot be found. return False + + +def module_dir(module): + """ + Find the name of the directory that contains a module, if possible. + + Raise ValueError otherwise, e.g. for namespace packages that are split + over several directories. + """ + # Convert to list because _NamespacePath does not support indexing on 3.3. + paths = list(getattr(module, '__path__', [])) + if len(paths) == 1: + return paths[0] + else: + filename = getattr(module, '__file__', None) + if filename is not None: + return os.path.dirname(filename) + raise ValueError("Cannot determine directory containing %s" % module) |
