Skip to main content

Python library for requesting root privileges

Project description

Raiseup

Raiseup is a small Python library(a fork of elevate) that re-launches the current process with root/admin privileges using one of the following mechanisms:

  • UAC (Windows)
  • AppleScript (macOS)
  • pkexec, gksudo, kdesudo or doas (Linux and FreeBSD)
  • sudo (Linux, macOS)

Usage

To use, call raiseup.elevate(file_path) early in your script(where file_path is the absolute path of the script that needs to be elevated). When run as root this function does nothing. When not run as root, this function replaces the current process (Linux, macOS) or creates a new process, waits, and exits (Windows).

Consider the following example(Note: os.getuid() only works on POSIX based systems. For windows, you might need to find other ways to check which user this script is currently running as):

import os
def is_root():
    return os.getuid() == 0
if not is_root():
    from raiseup import elevate
    print("Not root")
    elevate(__file__)
else:
    print("This script is running as root")

This prints:

Not root
This script is running as root

On Windows, the new process's standard streams are not attached to the parent, which is an inherent limitation of UAC. By default the new process runs in a new console window. To suppress this window, use elevate(file_path,show_console=False).

On Linux and macOS, graphical prompts are tried before sudo and doas, by default. To prevent graphical prompts, use elevate(file_path,graphical=False).

Improvements over Elevate:

  • Graphical environment variables $DISPLAY and $XAUTHORITY are automatically passed on to the elevated process (useful for GUI applications) on Linux and FreeBSD
  • Added support for doas and the FreeBSD operating system
  • Older code has been cleaned up
  • Broken code has been fixed

PyPI Package:

Raiseup is available to be installed from PyPI here

pip install raiseup

A common mistake:

Avoid importing raiseup unconditionally in your scripts if the raiseup package is not installed for the root/administrator user. Otherwise, the raiseup package will be imported regradless of which user is running the script. If the root user is running the script, it will fail to find raiseup resulting in a ModuleNotFoundError and script termination.

(c) 2018 Barney Gale and (c) 2021 Xploreinfinity

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

raiseup-0.2.tar.gz (4.8 kB view hashes)

Uploaded Source

Built Distribution

raiseup-0.2-py3-none-any.whl (5.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page