MCPcopy Index your code
hub / github.com/python/cpython / extend_path

Function extend_path

Lib/pkgutil.py:264–355  ·  view source on GitHub ↗

Extend a package's path. Intended use is to place the following code in a package's __init__.py: from pkgutil import extend_path __path__ = extend_path(__path__, __name__) For each directory on sys.path that has a subdirectory that matches the package name, add the sub

(path, name)

Source from the content-addressed store, hash-verified

262
263
264def extend_path(path, name):
265 """Extend a package's path.
266
267 Intended use is to place the following code in a package's __init__.py:
268
269 from pkgutil import extend_path
270 __path__ = extend_path(__path__, __name__)
271
272 For each directory on sys.path that has a subdirectory that
273 matches the package name, add the subdirectory to the package's
274 __path__. This is useful if one wants to distribute different
275 parts of a single logical package as multiple directories.
276
277 It also looks for *.pkg files beginning where * matches the name
278 argument. This feature is similar to *.pth files (see site.py),
279 except that it doesn't special-case lines starting with 'import'.
280 A *.pkg file is trusted at face value: apart from checking for
281 duplicates, all entries found in a *.pkg file are added to the
282 path, regardless of whether they are exist the filesystem. (This
283 is a feature.)
284
285 If the input path is not a list (as is the case for frozen
286 packages) it is returned unchanged. The input path is not
287 modified; an extended copy is returned. Items are only appended
288 to the copy at the end.
289
290 It is assumed that sys.path is a sequence. Items of sys.path that
291 are not (unicode or 8-bit) strings referring to existing
292 directories are ignored. Unicode items of sys.path that cause
293 errors when used as filenames may cause this function to raise an
294 exception (in line with os.path.isdir() behavior).
295 """
296
297 if not isinstance(path, list):
298 # This could happen e.g. when this is called from inside a
299 # frozen package. Return the path unchanged in that case.
300 return path
301
302 sname_pkg = name + ".pkg"
303
304 path = path[:] # Start with a copy of the existing path
305
306 parent_package, _, final_name = name.rpartition('.')
307 if parent_package:
308 try:
309 search_path = sys.modules[parent_package].__path__
310 except (KeyError, AttributeError):
311 # We can't do anything: find_loader() returns None when
312 # passed a dotted name.
313 return path
314 else:
315 search_path = sys.path
316
317 for dir in search_path:
318 if not isinstance(dir, str):
319 continue
320
321 finder = get_importer(dir)

Callers

nothing calls this directly

Calls 10

get_importerFunction · 0.85
openFunction · 0.70
rpartitionMethod · 0.45
find_specMethod · 0.45
appendMethod · 0.45
joinMethod · 0.45
isfileMethod · 0.45
writeMethod · 0.45
rstripMethod · 0.45
startswithMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…