Detect if Python running as sudo
On Unix-like systems such as Linux and macOS, Python can detect if a script is being run as sudo / root from the UID, which is by definition zero for root.
import os def running_as_root() -> bool: return os.getuid() == 0
or for demonstation as a one-liner:
python3 -c "import os; print(os.getuid() == 0)"