Python: 应用程序和终端之间的通信要求管理员密码

Python: communication between an app and terminal which is asking for Admins password

我正在尝试完成以下但没有成功,有什么建议吗? ⇒ 我的应用程序将 运行 一个需要管理员密码的命令,如果你 运行 终端中的命令,该命令将停止询问密码,但是,如果你 运行 它在 Python,它只会跳过它并以错误结束(因为没有输入管理员密码。我已经使用 elevate 模块以 root 身份启动应用程序,但是,命令 im使用不允许 运行 作为根 :

Do not run this script with root privileges. Do not use 'sudo'

当 cli 正在等待管理员密码而不是跳过它时,关于如何在 cli 和 python 之间进行通信的任何建议? 谢谢大家

我的代码:

Import os

os.system('killall Box')
os.system('/Library/Application\ Support/Box/uninstall_box_drive')

结果:

    /usr/local/bin/python3.10 /Users/user/PycharmProjects/pythonProjectBoxUnsaved/venv/test.py
No matching processes belonging to you were found
Unload failed: 5: Input/output error
Try running `launchctl bootout` as root for richer errors.
0
/usr/bin/fileproviderctl
File provider com.box.desktop.boxfileprovider/Box not found. Available providers:
 - iCloud Drive (hidden)
    com.apple.CloudDocs.MobileDocumentsFileProvider
    ~/L{5}y/M{14}s
fileproviderctl: can't find domain for com.box.desktop.boxfileprovider/Box: (null)
No matching processes belonging to you were found
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required

* * * * * *

Process finished with exit code 0

Error when trying to run the code

Error when using 'elevate' module

我也试过pexpect,不行,代码如下:

import os
import pexpect

os.system('killall Box')
child = pexpect.spawn('/Library/Application\ Support/Box/uninstall_box_drive')
print(child.after)
child.expect_exact('Password:')
print(child.after)
child.sendline('my_password')
print(child.after)

结果如下:

None

b'密码:'

b'密码:'

解决了。

必须在生成参数中包含 Bash 路径:

cmd = "/Library/Application\ Support/Box/uninstall_box_drive"
child = pexpect.spawn("/bin/bash", ["-c", cmd])