Basj
Basj

Reputation: 46463

As a Windows 10 administrator, how to run a command as administrator from Python?

Running this Python code in Windows 7 from an administrator account perfectly works:

import os
os.system('netsh interface set interface "Ethernet" enable')

Running the same code in Windows 10 from an admin account gives this error:

The requested operation requires elevation (Run as administrator)

How to make this work on Windows 10?

Upvotes: 0

Views: 603

Answers (1)

wasif
wasif

Reputation: 15478

You can use win32com.shell.shell module:

import win32com.shell.shell as shell
commands = 'interface set interface "Ethernet" enable'
shell.ShellExecuteEx(lpVerb='runas', lpFile='netsh.exe', lpParameters=commands)

If you have problems to import this module see here

Upvotes: 2

Related Questions