开发者

Python: How to start a process with Administrator Permissions?

开发者 https://www.devze.com 2023-02-04 11:06 出处:网络
I am starting the following script from a Windows 7 command line with administrator permissions: import win32com.client

I am starting the following script from a Windows 7 command line with administrator permissions:

import win32com.client
import time
import SendKeys
import os
from ctypes import *

shell = win32com.client.Dispatch("WScript.Shell")

os.startfile("C:\...exe")

I have also assigned the feat开发者_JAVA百科ure 'Run this programme as an administrator' to python.exe under Properties > Compatibility > Privilege Level. This did not change anything.

The programme still behaves differently when opened this way to how it behaves when I just open it via a double click on screen. Am I missing some important bit here? Will the process invoked in this way not be run as if started with administrator privileges?

Thanks for your help in advance!

Cheers -

Pat


I don't have access to Vista or Windows 7, but you should be able to use the runas command.

import subprocess
subprocess.call(['runas', '/user:Administrator', 'C:/my_program.exe'])


OK ... I figured out what the problem was. It actually had nothing to do with permissions, contrary to my initial suspicion. Sorry about that!

The reason why the application did not work correctly was because the Python script was located and invoked in another directory. For that reason, some of the application's dependencies were not referenced correctly and it couldn't find some of the files it needs to run properly. Moving the python script into the same directory as the invoked application was one way to fix that.

Sorry again for the misleading initial interpretation of what seemed to be the matter.

0

精彩评论

暂无评论...
验证码 换一张
取 消