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()
