Request UAC elevation from within a Python script? Request UAC elevation from within a Python script? windows windows

Request UAC elevation from within a Python script?


As of 2017, an easy method to achieve this is the following:

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

If you are using Python 2.x, then you should replace the last line for:

ctypes.windll.shell32.ShellExecuteW(None, u"runas", unicode(sys.executable), unicode(" ".join(sys.argv)), None, 1)

Also note that if you converted you python script into an executable file (using tools like py2exe, cx_freeze, pyinstaller) then you should use sys.argv[1:] instead of sys.argv in the fourth parameter.

Some of the advantages here are:

  • No external libraries required. It only uses ctypes and sys from standard library.
  • Works on both Python 2 and Python 3.
  • There is no need to modify the file resources nor creating a manifest file.
  • If you don't add code below if/else statement, the code won't ever be executed twice.
  • You can get the return value of the API call in the last line and take an action if it fails (code <= 32). Check possible return values here.
  • You can change the display method of the spawned process modifying the sixth parameter.

Documentation for the underlying ShellExecute call is here.


It took me a little while to get dguaraglia's answer working, so in the interest of saving others time, here's what I did to implement this idea:

import osimport sysimport win32com.shell.shell as shellASADMIN = 'asadmin'if sys.argv[-1] != ASADMIN:    script = os.path.abspath(sys.argv[0])    params = ' '.join([script] + sys.argv[1:] + [ASADMIN])    shell.ShellExecuteEx(lpVerb='runas', lpFile=sys.executable, lpParameters=params)    sys.exit(0)


It seems there's no way to elevate the application privileges for a while for you to perform a particular task. Windows needs to know at the start of the program whether the application requires certain privileges, and will ask the user to confirm when the application performs any tasks that need those privileges. There are two ways to do this:

  1. Write a manifest file that tells Windows the application might require some privileges
  2. Run the application with elevated privileges from inside another program

This two articles explain in much more detail how this works.

What I'd do, if you don't want to write a nasty ctypes wrapper for the CreateElevatedProcess API, is use the ShellExecuteEx trick explained in the Code Project article (Pywin32 comes with a wrapper for ShellExecute). How? Something like this:

When your program starts, it checks if it has Administrator privileges, if it doesn't it runs itself using the ShellExecute trick and exits immediately, if it does, it performs the task at hand.

As you describe your program as a "script", I suppose that's enough for your needs.

Cheers.