Python administrator privileges [closed]

1

Is there any way or command to run a Python script with admin privileges?

    
asked by anonymous 02.01.2017 / 18:19

1 answer

4

If you are on Linux, just run with sudo:

sudo python meuScript.py

In the case of windows (since you mentioned administrator instead of root, I suppose that is the case), there are some options:

  • You can run the terminal with administrator privileges.

    Start - > cmd - > right click - > Run as administrator.

  • You can also press CTRL + SHIFT + ENTER after typing cmd. After that, just run the script normally.

  • Make the script itself request for administrator privileges:
  •     import os
        import sys
        import win32com.shell.shell 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)

    Source: StackOverflow

        
    02.01.2017 / 18:39