def patch_shebang_line_py(fname, to_movable=True, targetdir=""):
    """Changes shebang line in '.py' file to relative or absolue path"""
    import fileinput
    exec_path = r'#!.\python.exe' if to_movable else '#!' + sys.executable
    if 'pypy3' in sys.executable:
        exec_path = r'#!.\pypy3.exe' if to_movable else exec_path
    for line in fileinput.input(fname, inplace=True):
        if re.match(r'^#\!.*python\.exe$', line) or re.match(r'^#\!.*pypy3\.exe$', line):
            print(exec_path)
        else:
            print(line, end='')
Changes shebang line in '.py' file to relative or absolue path
winpython/utils.py