The `selinux` library for Python provides a shim for interacting with the system's SELinux module. It allows Python applications to query SELinux status, manage security contexts, and perform other SELinux-related operations. It primarily acts as a wrapper around the system's native `_selinux` module, which is typically provided by the `libselinux-python` package on Linux distributions. The current version is 0.3.0, with an active release cadence driven by contributions.
pip install selinuxVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to import the `selinux` module and check if SELinux is enabled on the system, along with its current enforcement status and policy version. This code will gracefully handle systems where SELinux is not active.
Upgrade your Python environment to 3.9 or newer, or pin your `selinux` dependency to `<0.3.0` if maintaining an older Python version.
Ensure your system has SELinux enabled and the `libselinux-python` (or equivalent) package installed via your OS package manager (e.g., `dnf install libselinux-python` on RHEL/CentOS, `apt install python3-selinux` on Debian/Ubuntu, though the latter might be for a different package).
Run your Python script with appropriate privileges if it needs to perform privileged SELinux operations. Always follow the principle of least privilege.
Always explicitly check `selinux.is_selinux_enabled()` before attempting core SELinux operations. Implement robust error handling for functions that interact with SELinux.
No dependency data recorded yet.