Returns the sequence of directories that will be searched for the named executable (similar to a shell) when launching a process. *env* must be an environment variable dict or None. If *env* is None, os.environ will be used.
(env=None)
| 660 | |
| 661 | |
| 662 | def get_exec_path(env=None): |
| 663 | """Returns the sequence of directories that will be searched for the |
| 664 | named executable (similar to a shell) when launching a process. |
| 665 | |
| 666 | *env* must be an environment variable dict or None. If *env* is None, |
| 667 | os.environ will be used. |
| 668 | """ |
| 669 | # Use a local import instead of a global import to limit the number of |
| 670 | # modules loaded at startup: the os module is always loaded at startup by |
| 671 | # Python. It may also avoid a bootstrap issue. |
| 672 | import warnings |
| 673 | |
| 674 | if env is None: |
| 675 | env = environ |
| 676 | |
| 677 | # {b'PATH': ...}.get('PATH') and {'PATH': ...}.get(b'PATH') emit a |
| 678 | # BytesWarning when using python -b or python -bb: ignore the warning |
| 679 | with warnings.catch_warnings(): |
| 680 | warnings.simplefilter("ignore", BytesWarning) |
| 681 | |
| 682 | try: |
| 683 | path_list = env.get('PATH') |
| 684 | except TypeError: |
| 685 | path_list = None |
| 686 | |
| 687 | if supports_bytes_environ: |
| 688 | try: |
| 689 | path_listb = env[b'PATH'] |
| 690 | except (KeyError, TypeError): |
| 691 | pass |
| 692 | else: |
| 693 | if path_list is not None: |
| 694 | raise ValueError( |
| 695 | "env cannot contain 'PATH' and b'PATH' keys") |
| 696 | path_list = path_listb |
| 697 | |
| 698 | if path_list is not None and isinstance(path_list, bytes): |
| 699 | path_list = fsdecode(path_list) |
| 700 | |
| 701 | if path_list is None: |
| 702 | path_list = defpath |
| 703 | return path_list.split(pathsep) |
| 704 | |
| 705 | |
| 706 | # Change environ to automatically call putenv() and unsetenv() |