我正在编写一个需要执行管理任务的PYQT应用程序。我希望以提升特权开始我的脚本。我知道这个问题在So或其他论坛中多次提出。但是人们建议的解决方案是要看看这个问题
请求UAC高程来自python脚本吗?
但是,I无法执行链接中给出的示例代码。我已经将此代码放在主文件的顶部,并试图执行它。
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)
sys.exit(0)
print "I am root now."
它实际上要求提升的权限,但打印行永远不会被执行。有人可以帮助我成功运行上述代码。
I am writing a pyqt application which require to execute admin task. I would prefer to start my script with elevate privilege. I am aware that this question is asked many times in SO or in other forum. But the solution people are suggesting is to have a look at this SO question
Request UAC elevation from within a Python script?
However, I am unable to execute the sample code given in the link. I have put this code on top of the main file and tried to execute it.
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)
sys.exit(0)
print "I am root now."
It actually ask permission to elevate but print line never get executed. Somebody can help me to run the above code successfully.
发布评论
评论(15)
确保您有Python的路径,如果没有,请赢得键 + R,输入“%appdata%”(无Qotes)打开本地目录,然后转到程序目录,打开Python,然后选择您的Python版本目录。单击文件选项卡,然后选择复制路径,然后关闭文件资源管理器。
然后再次赢得键 + r,键入控制并击中输入。搜索环境变量。单击结果,您将获得一个窗口。在右下角,单击环境变量。在系统侧找到路径,选择它,然后单击编辑。
在新窗口中,单击新窗口并粘贴那里的路径。单击确定,然后在第一个窗口中应用。重新启动您的电脑。然后最后一次win + r键入cmd,然后do ctrl + shift + enter。授予PREPILIGES并打开文件资源管理器,获取您的脚本并复制其路径。返回CMD,输入“ Python”,然后粘贴路径并击中Enter。完毕
Make sure you have python in path,if not,win key + r, type in "%appdata%"(without the qotes) open local directory, then go to Programs directory ,open python and then select your python version directory. Click on file tab and select copy path and close file explorer.
Then do win key + r again, type control and hit enter. search for environment variables. click on the result, you will get a window. In the bottom right corner click on environmental variables. In the system side find path, select it and click on edit.
In the new window, click on new and paste the path in there. Click ok and then apply in the first window. Restart your PC. Then do win + r for the last time, type cmd and do ctrl + shift + enter. Grant the previliges and open file explorer, goto your script and copy its path. Go back into cmd , type in "python" and paste the path and hit enter. Done
我想要一个增强的版本,因此我最终获得了一个允许的模块:
UAC请求(如果需要),请从非特权实例(使用IPC和网络端口)和其他一些糖果进行打印和记录。用法只是在您的脚本中插入leverteme():在非特权中,它会收听特权打印/日志,然后退出返回false的返回,在特权实例中,它立即返回true。
支持Pyinstaller。
原型:
winadmin.py
I wanted a more enhanced version so I ended up with a module which allows:
UAC request if needed, printing and logging from nonprivileged instance (uses ipc and a network port) and some other candies. usage is just insert elevateme() in your script: in nonprivileged it listen for privileged print/logs and then exits returning false, in privileged instance it returns true immediately.
Supports pyinstaller.
prototype:
winadmin.py
值得一提的是,如果您打算用 pyinstaller ,并希望避免支持该功能,并希望通过您自己,您可以传递
-UAC-ADMIN
或-UAC-UIACCESS
参数,以便在开始时请求UAC高程。It worth mentioning that if you intend to package your application with PyInstaller and wish to avoid supporting that feature by yourself, you can pass the
--uac-admin
or--uac-uiaccess
argument in order to request UAC elevation on start.jetbrains' winelevator> winelevator 可用在这里特权同时保持stdin/stdout/stderr完整:
JetBrains' WinElevator (signed elevator.exe and launcher.exe available here) allows you to spawn a subprocess that requests elevated privileges while keeping stdin/stdout/stderr intact:
另外,如果您的工作目录与您可以使用的LPDirectory不同,
如果更改路径不是理想的选择,
删除python 3.x的Unicode
Also if your working directory is different than you can use lpDirectory
Will come handy if changing the path is not a desirable option
remove unicode for python 3.X
这对我有用:
以下是我用于上述代码的参考文献:
htttps>控制 - applications-via-sendkeys.html
This worked for me:
Below are the references I used for above code:
https://win32com.goermezer.de/microsoft/windows/controlling-applications-via-sendkeys.html
https://www.oreilly.com/library/view/python-cookbook/0596001673/ch07s16.html
使用pyuac
这是Preston Landers对Orignal Admin脚本的更新
链接到Python项目: https://pypi.org/project/project/project/pyuac/
github: https://github.com/preston-landers/pyuac/pyuac/pyuac
这对我有用
Use pyuac
it is the update to the orignal admin Script by Preston Landers
Link to python Projects: https://pypi.org/project/pyuac/
Github: https://github.com/Preston-Landers/pyuac
This worked for me it
我可以确认Delphifirst的解决方案起作用,并且是运行具有提升特权的Python脚本的问题最简单,最简单的解决方案。
我为Python可执行文件(Python.exe)创建了一个快捷方式,然后通过在python.exe后添加脚本的名称来修改快捷方式。接下来,我在快捷方式的“兼容性选项卡”上检查了“作为管理员”。执行快捷方式后,您将获得及时征求脚本作为管理员运行脚本的权限。
我的特定Python应用程序是一个安装程序程序。该程序允许安装和卸载另一个Python应用程序。就我而言,我创建了两个快捷方式,一个名为“ AppName install”,另一个名为“ AppName卸载”。两个快捷方式之间的唯一区别是python脚本名称的参数。在安装程序版本中,参数为“安装”。在卸载版本中,参数是“卸载”。安装程序脚本中的代码评估所提供的参数,并根据需要调用适当的函数(安装或卸载)。
我希望我的解释能够帮助其他人更快地弄清楚如何运行具有提高特权的Python脚本。
I can confirm that the solution by delphifirst works and is the easiest, simplest solution to the problem of running a python script with elevated privileges.
I created a shortcut to the python executable (python.exe) and then modified the shortcut by adding my script's name after the call to python.exe. Next I checked "run as administrator" on the "compatibility tab" of the shortcut. When the shortcut is executed, you get a prompt asking permission to run the script as an administrator.
My particular python application was an installer program. The program allows installing and uninstalling another python app. In my case I created two shortcuts, one named "appname install" and the other named "appname uninstall". The only difference between the two shortcuts is the argument following the python script name. In the installer version the argument is "install". In the uninstall version the argument is "uninstall". Code in the installer script evaluates the argument supplied and calls the appropriate function (install or uninstall) as needed.
I hope my explanation helps others more quickly figure out how to run a python script with elevated privileges.
这是带有stdout重定向的解决方案:
Here is a solution with an stdout redirection:
更新截至19-02-2023
以下脚本的更新现在由同一位作者作为Python软件包。您可以从PYPI中安装它,该pypi居住在 https://pypi.org/project/project/pyuac/ ,源代码/主页位于 https://github.com/preston-landers/pyuac < /a>。使用:
包装的直接使用是:
或者,如果您想使用装饰器:
原始答案
谢谢大家的答复。我的脚本与Preston Landers在2010年撰写的模块/脚本一起工作。在浏览了两天的互联网之后,我可以找到脚本。它深深地隐藏在Pywin32邮寄列表中。使用此脚本,更容易检查用户是否是管理员,如果不是管理员,请要求使用UAC/Admin特权。它在单独的窗口中提供输出以显示代码正在执行的操作。脚本中还包括一个如何使用代码的示例。为了使每个在Windows上寻找UAC的人都受益,请查看此代码。它可以从您的主要脚本中使用类似的东西: -
实际代码(在模块中)为: -
Update as of 19-02-2023
The update to the below script is now alive as a Python package by the same author. You can install it from PyPi, which lives at https://pypi.org/project/pyuac/, and the source code/home page is located at https://github.com/Preston-Landers/pyuac. Install it using:
Direct usage of the package is:
Or, if you wish to use the decorator:
Original answer
Thank you all for your reply. I got my script working with the module/script written by Preston Landers in 2010. After two days of browsing the internet, I could find the script. It was deeply hidden in the pywin32 mailing list. With this script, it is easier to check if the user is an admin, and if not, ask for UAC/admin privileges. It provides output in separate windows to display what the code is doing. An example of how to use the code is also included in the script. For the benefit of everyone who's looking for UAC on Windows, take a look at this code. It can be used something like this from your main script:-
The actual code (in the module) is:-
在对。因此,即使代码可能正常,您也不会看到“我现在是root”。
不要打印某些内容,而是尝试写入文件:
然后查看文件。
in comments to the answer you took the code from someone says ShellExecuteEx doesn't post its STDOUT back to the originating shell. so you will not see "I am root now", even though the code is probably working fine.
instead of printing something, try writing to a file:
and then look in the file.
我找到了一个非常简单的解决方案解决这个问题。
python.exe
创建一个快捷方式,c:\ xxx \ ... \ python.exe your_script.py
我不确定这些选项的咒语是否正确,因为我正在使用中文版本的Windows。
I found a very easy solution to this problem.
python.exe
C:\xxx\...\python.exe your_script.py
I'm not sure whether the spells of these options are right, since I'm using Chinese version of Windows.
这是一个仅需要CTYPES模块的解决方案。支持Pyinstaller包装程序。
Here is a solution which needed ctypes module only. Support pyinstaller wrapped program.