MCPcopy Create free account
hub / github.com/TencentARC/BrushNet / check_imports

Function check_imports

src/diffusers/utils/dynamic_modules_utils.py:127–156  ·  view source on GitHub ↗

Check if the current Python environment contains all the libraries that are imported in a file.

(filename)

Source from the content-addressed store, hash-verified

125
126
127def check_imports(filename):
128 """
129 Check if the current Python environment contains all the libraries that are imported in a file.
130 """
131 with open(filename, "r", encoding="utf-8") as f:
132 content = f.read()
133
134 # Imports of the form `import xxx`
135 imports = re.findall(r"^\s*import\s+(\S+)\s*$", content, flags=re.MULTILINE)
136 # Imports of the form `from xxx import yyy`
137 imports += re.findall(r"^\s*from\s+(\S+)\s+import", content, flags=re.MULTILINE)
138 # Only keep the top-level module
139 imports = [imp.split(".")[0] for imp in imports if not imp.startswith(".")]
140
141 # Unique-ify and test we got them all
142 imports = list(set(imports))
143 missing_packages = []
144 for imp in imports:
145 try:
146 importlib.import_module(imp)
147 except ImportError:
148 missing_packages.append(imp)
149
150 if len(missing_packages) > 0:
151 raise ImportError(
152 "This modeling file requires the following packages that were not found in your environment: "
153 f"{', '.join(missing_packages)}. Run `pip install {' '.join(missing_packages)}`"
154 )
155
156 return get_relative_imports(filename)
157
158
159def get_class_in_module(class_name, module_path):

Callers 1

get_cached_module_fileFunction · 0.85

Calls 1

get_relative_importsFunction · 0.85

Tested by

no test coverage detected