def _replace_shebang(path: Path, new_executable: bytes) -> None:
    """Replace the python executable from the shebang line, which can be in two forms:

    1. #!python_executable
    2. #!/bin/sh
       '''exec' '/path to/python' "$0" "$@"
       ' '''
    """
    _complex_shebang_re = rb"^(#!/bin/sh\n'''exec' )('.+?')( \"\$0\")"
    _simple_shebang_re = rb"^(#!)(.+?)\s*(?=\n)"
    contents = path.read_bytes()

    if not _is_console_script(contents):
        return

    if os.name == "nt":
        new_content, count = re.subn(_simple_shebang_re, rb"\1" + new_executable, contents, count=1, flags=re.M)
        if count > 0:
            path.write_bytes(new_content)
        return

    new_content, count = re.subn(_complex_shebang_re, rb"\1" + new_executable + rb"\3", contents, count=1)
    if count > 0:
        path.write_bytes(new_content)
        return

    new_content, count = re.subn(_simple_shebang_re, rb"\1" + new_executable, contents, count=1)
    if count > 0:
        path.write_bytes(new_content)
Replace the python executable from the shebang line, which can be in two forms:

    1. #!python_executable
    2. #!/bin/sh
       '''exec' '/path to/python' "$0" "$@"
       ' '''
    
src/pdm/environments/local.py