Run python script as admin

python script run as admin:

import ctypes, sys
def is_admin():
    try:
        return ctypes.windll.shell32.IsUserAnAdmin()
    except:
        return False
if is_admin():
    # Code of your program here
else:
    # Re-run the program with admin rights
    ctypes.windll.shell32.ShellExecuteW(None, "runas", sys.executable, " ".join(sys.argv), None, 1)

 

Reference:

1. run python script as admin visual studio code Code Example

posted @ 2022-03-25 15:36  余生以学  阅读(119)  评论(0编辑  收藏  举报