System Module
Provides OS-level integration and system environment utilities.
Features include OS and hardware detection, environment PATH manipulation,
privilege elevation, process restarting, and runtime dependency checking.
is_elevated()
def is_elevated() -> boolWhether the current process has elevated privileges or not.
is_unix()
def is_unix() -> boolWhether the current operating system is a Unix-like OS (Linux, macOS, BSD, …) or not.
get_os_name()
def get_os_name() -> strThe name of the operating system (e.g., Windows, Linux, …).
get_architecture()
def get_architecture() -> strThe CPU architecture (e.g., x86_64, ARM, …).
get_python_version()
def get_python_version() -> strThe version string of the currently running Python interpreter (e.g., 3.10.4).
get_env_path()
def get_env_path(*, as_list: bool = False) -> Path | list[Path]Get the PATH environment variable.
as_list– If true, returns the paths as a list ofPaths; otherwise, as a singlePath.
has_env_path()
def has_env_path(path: Path | str | None = None, /, *, cwd: bool = False, base_dir: bool = False) -> boolCheck if a path is present in the PATH environment variable.
path– The path to check for.cwd– If true, uses the current working directory as the path.base_dir– If true, uses the script's base directory as the path.
add_env_path()
def add_env_path(path: Path | str | None = None, /, *, cwd: bool = False, base_dir: bool = False) -> NoneAdd a path to the PATH environment variable.
path– The path to add.cwd– If true, uses the current working directory as the path.base_dir– If true, uses the script's base directory as the path.
remove_env_path()
def remove_env_path(path: Path | str | None = None, /, *, cwd: bool = False, base_dir: bool = False) -> NoneRemove a path from the PATH environment variable.
path– The path to remove.cwd– If true, uses the current working directory as the path.base_dir– If true, uses the script's base directory as the path.
elevate()
def elevate(win_title: str | None = None, args: Sequence[str] | None = None) -> boolAttempts to start a new process with elevated privileges.
win_title– The window title of the elevated process (only on Windows).args– A list of additional arguments to be passed to the elevated process.
After the elevated process started, the original process will exit.
This means, that this method has to be run at the beginning of the program or
or else the program has to continue in a new window after elevation.
Returns True if the current process already has elevated privileges
and raises a PermissionError if the user denied the elevation or the elevation failed.
Example Usage
import xulbux as xx
# Request administrator / root privileges:
if xx.system.elevate(win_title="Elevated Setup"):
print("Running with elevated permissions.")
else:
print("Elevation failed or was denied.")restart()
def restart(prompt: object = "", /, *, wait: int = 0, continue_program: bool = False, force: bool = False) -> NoneRestarts the system with some advanced options
prompt– The message to be displayed in the systems restart notification.wait– The time to wait until restarting in seconds.continue_program– Whether to continue the current Python program after calling this function.force– Whether to force a restart even if other processes are still running.
check_libs()
def check_libs(
lib_names: list[str],
/,
*,
install_missing: bool = False,
missing_libs_msgs: MissingLibsMsgs | None = None,
confirm_install: bool = True,
) -> list[str] | NoneChecks if the given list of libraries are installed and optionally installs the missing ones.
lib_names– A list of library names to check.install_missing– Whether to directly install missing libraries using pip.missing_libs_msgs– Two messages:- The first one is displayed when missing libraries are found.
- The second one is the confirmation message before installing missing libraries.
confirm_install– Whether the user will be asked for confirmation before installing missing libraries.
If some libraries are missing or they could not be installed,
their names will be returned as a list.
If all libraries are installed (or were installed successfully), None will be returned.
Example Usage
import xulbux as xx
# Check and prompt to install missing packages:
missing = xx.system.check_libs(
["requests", "pytest"],
install_missing=True,
confirm_install=True,
)