def patch_shebang_line(fname, pad=b' ', to_movable=True, targetdir=""):

    import re
    import sys
    import os
    target_dir = targetdir # movable option
    if to_movable == False:
        target_dir = os.path.abspath(os.path.dirname(fname))
        target_dir = os.path.abspath(os.path.join(target_dir, r'..')) + '\\'

    executable= sys.executable
    if sys.version_info[0] == 2:
        shebang_line = re.compile(r"(#!.*pythonw?\.exe)")  # Python2.7
    else:
        shebang_line = re.compile(b"(#!.*pythonw?\.exe)")  # Python3+
        target_dir = target_dir.encode('utf-8')
    with open(fname, 'rb') as fh:
        initial_content = fh.read()
        fh.close
        fh = None
    content = shebang_line.split(initial_content, maxsplit=1)
    if len(content) != 3:
        return

    exe = os.path.basename(content[1][2:])
    content[1] = b'#!' + target_dir + exe #+ (pad * (len(content[1]) - len(exe) - 2))
    final_content = b''.join(content)
    if initial_content == final_content:
        return
    try:
        with open(fname, 'wb') as fo:
            fo.write(final_content)
            fo.close
            fo = None
            print("patched", fname)
    except Exception:
        print("failed to patch", fname)


# =============================================================================
# Patch sourcefile (instead of forking packages)
# =============================================================================
Returns a list of all dynamic properties defined for instance.
dataset/ETHPy150Open winpython/winpython/winpython/utils.py/patch_shebang_line--crossover with--dataset/ETHPy150Open CollabQ/CollabQ/.google_appengine/google/appengine/ext/db/__init__.py/Model.dynamic_properties