Skip to content

Instantly share code, notes, and snippets.

@aweffr
Created April 13, 2019 08:17
Show Gist options
  • Save aweffr/22e94f9ab4efe809f3a5d2488172809c to your computer and use it in GitHub Desktop.
Save aweffr/22e94f9ab4efe809f3a5d2488172809c to your computer and use it in GitHub Desktop.
python: windows 提升到 admin
def run_as_admin(argv=None, debug=False):
shell32 = ctypes.windll.shell32
if argv is None and shell32.IsUserAnAdmin():
return True
if argv is None:
argv = sys.argv
if hasattr(sys, '_MEIPASS'):
# Support pyinstaller wrapped program.
arguments = list(map(str, argv[1:]))
else:
arguments = list(map(str, argv))
argument_line = ' '.join(arguments)
executable = str(sys.executable)
if debug:
print('Command line: ', executable, argument_line)
ret = shell32.ShellExecuteW(None, "runas", executable, argument_line, None, 1)
if int(ret) <= 32:
return False
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment