Python: How to start a process with Administrator Permissions?

只愿长相守 提交于 2019-12-06 05:35:10

问题


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 feature '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


回答1:


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'])



回答2:


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.



来源:https://stackoverflow.com/questions/4645909/python-how-to-start-a-process-with-administrator-permissions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!