1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 import sys, imp, threading
21
23 """Import a named module, searching within a list of paths.
24
25 Supports loading of hierarchical modules from a list of paths, returning a reference to the loaded module.
26 Reloading of the module can be requested should the module already exist in sys.modules. Note that in a
27 module named X.Y.Z, should reload be set to True only the tail module, X.Y.Z, will be reloaded on import;
28 the intervening modules, X and X.Y, will not be reloaded.
29
30 @param name: The module name
31 @param path: A list of paths to search for the module
32 @param reload: A boolean indicating if the module should be reloaded if already in sys.modules
33
34 """
35 elements = name.split(".")
36 module = __import_module(elements[0], elements[0], None, path, reload and elements[0] == name)
37 if not module: raise ImportError, "No module named " + name
38
39 if len(elements) > 1:
40 for element in elements[1:]:
41 fqname = "%s.%s" % (module.__name__, element)
42 module = __import_module(fqname, element, module, module and module.__path__, reload and fqname == name)
43 if not module: raise ImportError, "No module named " + fqname
44 return module
45
46
48 """Method to load a module.
49
50 @param fqname: The fully qualified name of the module
51 @param qname: The qualified name relative to the parent module (if one exists)
52 @param parent: A reference to the loaded parent module
53 @param path: The path used to search for the module
54 @param reload: Boolean indicating if the module should be reloaded if already in sys.modules
55
56 """
57 if not reload:
58 try:
59 return sys.modules[fqname]
60 except KeyError:
61 pass
62 try:
63 file, pathname, description = imp.find_module(qname, path)
64 except ImportError:
65 return None
66 try:
67 module = imp.load_module(fqname, file, pathname, description)
68 finally:
69 if file: file.close()
70 if parent: setattr(parent, qname, module)
71 return module
72