How can I do privileged actions requiring sudo/root using Python?
For example, writing or modifying a file as root. Is there a library or standard way of doing this?
Ideally CLI and GUI methods.
How can I do privileged actions requiring sudo/root using Python?
For example, writing or modifying a file as root. Is there a library or standard way of doing this?
Ideally CLI and GUI methods.
If you start as a non prived user then you would need to execute the appropriate sudo command using the subprocess module.
Oh, I see. Easy as that. I didn’t expect it to work so flawlessly.
Do you happen to know of any software using this in CLI or GUI?
I am considering this as solved however, because running sudo with subprocess.run is near equivalent to doing it in a shell.
Its very common to see suborocess.run used in python code, both cli an gui.
Are you looking for anything specific as an example?
No, I have an idea what to look around for now. I was specifically looking for pkexec
, to “pop” the GUI that other software uses. Thanks for your help.
You can do that too. If it’s a command you can run at the shell, you can invoke it from inside Python. Take your pick - sudo, pkexec, or anything else you please - they’ll work identically.