Python scripts that used to work now fail due to UAC unless run as administrator

I have several Python scripts that used to work perfectly fine when running them directly from the .py file.
Recently, they all started failing with a UAC-related error, and now they only work if I run them from an elevated terminal with administrator privileges.

What’s strange is that the scripts that are failing all share this same piece of code:

# Admin elevation
def is_admin() -> bool:
    try:
        return bool(ctypes.windll.shell32.IsUserAnAdmin())
    except Exception:
        return False


def elevate_and_relaunch():
    script_path = os.path.abspath(sys.argv[0])
    params = f'"{script_path}"'
    if len(sys.argv) > 1:
        params = params + " " + " ".join(f'"{a}"' for a in sys.argv[1:])
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, params, None, 1)


if not is_admin():
    elevate_and_relaunch()
    sys.exit()

Was there a Windows update?

The script clearly intends to do something with elevated permissions, or as admin. If it does something else as non-admin, then that non-admin stuff would be much better if it lived in a separate script (with no is_admin tests etc. confusing matters and muddying the waters).

I think this code is forcing the script to run as admin, even when you run it as a non-admin? Windows is supposed to get admin-permission from the user using a pop-up before elevating the permission. Seems like a pretty suss code pattern regardless.

Running the scripts as administrator sounds like a good solution, but then I don’t know your situation.