打包成EXE以后执行会提示你需要管理员运行
import ctypes
from win32comext.shell.shell import ShellExecuteEx
def is_user_admin():
""" 检查admin """
return ctypes.windll.shell32.IsUserAnAdmin()
def run_as_admin():
""" 管理员运行 """
script = os.path.abspath(sys.argv[0])
args = ' '.join(sys.argv[1:]) if len(sys.argv) > 1 else ''
ShellExecuteEx(lpFile=sys.executable, lpParameters=f"{script} {args}",
nShow=1, lpVerb='runas')
return
def Main():
if not is_user_admin():
run_as_admin()
return
if __name__ == '__main__':
Main()