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 details)

Uploaded Source

Built Distribution

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

Uploaded Python 3

File details

Details for the file raiseup-0.2.tar.gz.

File metadata

  • Download URL: raiseup-0.2.tar.gz
  • Upload date:
  • Size: 4.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for raiseup-0.2.tar.gz
Algorithm Hash digest
SHA256 a00d1a90c94b0603084e7b5ba3c98d44754f05cb2a387d57419c9d9d5d94b24e
MD5 80df6e9d6e509edf13030ff97cfa529b
BLAKE2b-256 095924b4935028a6ad6150ba778e7b6212c01625f95603a4f5ee39b3af840c87

See more details on using hashes here.

File details

Details for the file raiseup-0.2-py3-none-any.whl.

File metadata

  • Download URL: raiseup-0.2-py3-none-any.whl
  • Upload date:
  • Size: 5.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.9

File hashes

Hashes for raiseup-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9d02ce70927533cacabfaa5706c055b144063a739a86c936c8510742246b5a48
MD5 6f51b32461366c6d34e953b9990f60b6
BLAKE2b-256 3e6707ff5d67837c09d30fc847d2e3fd6d7e100ffa09f16944d26bfd6d6ad41d

See more details on using hashes here.

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