Request UAC elevation from within a Python script?

Posted on

Solving problem is about exposing yourself to as many situations as possible like Request UAC elevation from within a Python script? and practice these strategies over and over. With time, it becomes second nature and a natural way you approach any problems in general. Big or small, always start with a plan, use other strategies mentioned here till you are confident and ready to code the solution.
In this post, my aim is to share an overview the topic about Request UAC elevation from within a Python script?, which can be followed any time. Take easy to follow this discuss.

Request UAC elevation from within a Python script?

I want my Python script to copy files on Vista. When I run it from a normal cmd.exe window, no errors are generated, yet the files are NOT copied. If I run cmd.exe “as administator” and then run my script, it works fine.

This makes sense since User Account Control (UAC) normally prevents many file system actions.

Is there a way I can, from within a Python script, invoke a UAC elevation request (those dialogs that say something like “such and such app needs admin access, is this OK?”)

If that’s not possible, is there a way my script can at least detect that it is not elevated so it can fail gracefully?

Asked By: jwfearn


Answer #1:

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

import ctypes, sys
def is_admin():
        return ctypes.windll.shell32.IsUserAnAdmin()
        return False
if is_admin():
    # Code of your program here
    # 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.

Answer #2:

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 os
import sys
import as shell
ASADMIN = '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)
Answered By: Jorenko

Answer #3:

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.


Answered By: dguaraglia

Answer #4:

Just adding this answer in case others are directed here by Google Search as I was.
I used the elevate module in my Python script and the script executed with Administrator Privileges in Windows 10.

Answered By: Irving Moy

Answer #5:

The following example builds on MARTIN DE LA FUENTE SAAVEDRA’s excellent work and accepted answer. In particular, two enumerations are introduced. The first allows for easy specification of how an elevated program is to be opened, and the second helps when errors need to be easily identified. Please note that if you want all command line arguments passed to the new process, sys.argv[0] should probably be replaced with a function call: subprocess.list2cmdline(sys.argv).

#! /usr/bin/env python3
import ctypes
import enum
import subprocess
import sys
# Reference:
# noinspection SpellCheckingInspection
class SW(enum.IntEnum):
    HIDE = 0
    MAXIMIZE = 3
    MINIMIZE = 6
    RESTORE = 9
    SHOW = 5
    SHOWNA = 8
class ERROR(enum.IntEnum):
    ZERO = 0
    BAD_FORMAT = 11
    DDE_BUSY = 30
    DDE_FAIL = 29
    DDE_TIMEOUT = 28
    DLL_NOT_FOUND = 32
    NO_ASSOC = 31
    OOM = 8
    SHARE = 26
def bootstrap():
    if ctypes.windll.shell32.IsUserAnAdmin():
       # noinspection SpellCheckingInspection
        hinstance = ctypes.windll.shell32.ShellExecuteW(
        if hinstance <= 32:
            raise RuntimeError(ERROR(hinstance))
def main():
    # Your Code Here
    print(input('Echo: '))
if __name__ == '__main__':
Answered By: Noctis Skytower

Answer #6:

Recognizing this question was asked years ago, I think a more elegant solution is offered on github by frmdstryr using his module pywinutils:


import pythoncom
from import shell,shellcon
def copy(src,dst,flags=shellcon.FOF_NOCONFIRMATION):
    """ Copy files using the built in Windows File copy dialog
    Requires absolute paths. Does NOT create root destination folder if it doesn't exist.
    Overwrites and is recursive by default
    @see for flags available
    # @see IFileOperation
    pfo = pythoncom.CoCreateInstance(shell.CLSID_FileOperation,None,pythoncom.CLSCTX_ALL,shell.IID_IFileOperation)
    # Respond with Yes to All for any dialog
    # @see
    # Set the destionation folder
    dst = shell.SHCreateItemFromParsingName(dst,None,shell.IID_IShellItem)
    if type(src) not in (tuple,list):
        src = (src,)
    for f in src:
        item = shell.SHCreateItemFromParsingName(f,None,shell.IID_IShellItem)
        pfo.CopyItem(item,dst) # Schedule an operation to be performed
    # @see
    success = pfo.PerformOperations()
    # @see
    aborted = pfo.GetAnyOperationsAborted()
    return success is None and not aborted

This utilizes the COM interface and automatically indicates that admin privileges are needed with the familiar dialog prompt that you would see if you were copying into a directory where admin privileges are required and also provides the typical file progress dialog during the copy operation.

Answered By: KenV99

Answer #7:

This may not completely answer your question but you could also try using the Elevate Command Powertoy in order to run the script with elevated UAC privileges.

I think if you use it it would look like ‘elevate python’

Answered By: TinyGrasshopper

Answer #8:

You can make a shortcut somewhere and as the target use:
then under properties and advanced select run as administrator.

When the user executes the shortcut it will ask them to elevate the application.

Answered By: officialhopsof

Leave a Reply

Your email address will not be published. Required fields are marked *