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
(path, name)
| 504 | |
| 505 | |
| 506 | def extend_path(path, name): |
| 507 | """Extend a package's path. |
| 508 | |
| 509 | Intended use is to place the following code in a package's __init__.py: |
| 510 | |
| 511 | from pkgutil import extend_path |
| 512 | __path__ = extend_path(__path__, __name__) |
| 513 | |
| 514 | For each directory on sys.path that has a subdirectory that |
| 515 | matches the package name, add the subdirectory to the package's |
| 516 | __path__. This is useful if one wants to distribute different |
| 517 | parts of a single logical package as multiple directories. |
| 518 | |
| 519 | It also looks for *.pkg files beginning where * matches the name |
| 520 | argument. This feature is similar to *.pth files (see site.py), |
| 521 | except that it doesn't special-case lines starting with 'import'. |
| 522 | A *.pkg file is trusted at face value: apart from checking for |
| 523 | duplicates, all entries found in a *.pkg file are added to the |
| 524 | path, regardless of whether they are exist the filesystem. (This |
| 525 | is a feature.) |
| 526 | |
| 527 | If the input path is not a list (as is the case for frozen |
| 528 | packages) it is returned unchanged. The input path is not |
| 529 | modified; an extended copy is returned. Items are only appended |
| 530 | to the copy at the end. |
| 531 | |
| 532 | It is assumed that sys.path is a sequence. Items of sys.path that |
| 533 | are not (unicode or 8-bit) strings referring to existing |
| 534 | directories are ignored. Unicode items of sys.path that cause |
| 535 | errors when used as filenames may cause this function to raise an |
| 536 | exception (in line with os.path.isdir() behavior). |
| 537 | """ |
| 538 | |
| 539 | if not isinstance(path, list): |
| 540 | # This could happen e.g. when this is called from inside a |
| 541 | # frozen package. Return the path unchanged in that case. |
| 542 | return path |
| 543 | |
| 544 | sname_pkg = name + ".pkg" |
| 545 | |
| 546 | path = path[:] # Start with a copy of the existing path |
| 547 | |
| 548 | parent_package, _, final_name = name.rpartition('.') |
| 549 | if parent_package: |
| 550 | try: |
| 551 | search_path = sys.modules[parent_package].__path__ |
| 552 | except (KeyError, AttributeError): |
| 553 | # We can't do anything: find_loader() returns None when |
| 554 | # passed a dotted name. |
| 555 | return path |
| 556 | else: |
| 557 | search_path = sys.path |
| 558 | |
| 559 | for dir in search_path: |
| 560 | if not isinstance(dir, str): |
| 561 | continue |
| 562 | |
| 563 | finder = get_importer(dir) |
nothing calls this directly
no test coverage detected