Python administrator privileges

Asked

Viewed 1,552 times

1

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

  • Your question is too wide or doesn’t seem to be about programming.

  • Which operating system used? Are you facing any problem while running code normally?

1 answer

3


If you are on linux, just run sudo:

sudo python meuScript.py

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

  1. 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.

  1. Make script itself ask 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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.