Is there any way or command to run a Python script with admin privileges?
Is there any way or command to run a Python script with admin privileges?
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.
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