MCPcopy Create free account
hub / github.com/pallets/quart / prepare_import

Function prepare_import

src/quart/cli.py:203–229  ·  view source on GitHub ↗

Given a filename this will try to calculate the python path, add it to the search path and return the actual module name that is expected.

(path: str)

Source from the content-addressed store, hash-verified

201
202
203def prepare_import(path: str) -> str:
204 """Given a filename this will try to calculate the python path, add it
205 to the search path and return the actual module name that is expected.
206 """
207 path = os.path.realpath(path)
208
209 fname, ext = os.path.splitext(path)
210 if ext == ".py":
211 path = fname
212
213 if os.path.basename(path) == "__init__":
214 path = os.path.dirname(path)
215
216 module_name = []
217
218 # move up until outside package structure (no __init__.py)
219 while True:
220 path, name = os.path.split(path)
221 module_name.append(name)
222
223 if not os.path.exists(os.path.join(path, "__init__.py")):
224 break
225
226 if sys.path[0] != path:
227 sys.path.insert(0, path)
228
229 return ".".join(module_name[::-1])
230
231
232class ScriptInfo:

Callers 1

load_appMethod · 0.85

Calls 1

appendMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…