summaryrefslogtreecommitdiff
path: root/django/utils/module_loading.py
blob: b23336e9df7a85dc5938bdede0b125118ae23fa2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import os
import imp

def module_has_submodule(mod, submod_name):
    # If the module was loaded from an egg, __loader__ will be set and 
    # its find_module must be used to search for submodules.
    loader = getattr(mod, '__loader__', None)
    if loader:
        mod_path = "%s.%s" % (mod.__name__, submod_name)
        mod_path = mod_path[len(loader.prefix):]
        x = loader.find_module(mod_path)
        if x is None:
            # zipimport.zipimporter.find_module is documented to take
            # dotted paths but in fact through Pyton 2.7 is observed 
            # to require os.sep in place of dots...so try using os.sep
            # if the dotted path version failed to find the requested 
            # submodule.
            x = loader.find_module(mod_path.replace('.', os.sep))
        return x is not None

    try:
        imp.find_module(submod_name, mod.__path__)
        return True
    except ImportError:
        return False