Registry / auth-security / selinux

selinux

JSON →
library0.3.0pypypi✓ verified 21d ago

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 selinux
INSTALL
IMPORT
SIG · SELINUX
S
selinux
auth-securitypythonv0.3.0
Install
1.6s avg
Import
57ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.060s · 18MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.6s · import 0.054s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

selinux
import selinux

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.

import selinux if selinux.is_selinux_enabled(): print(f"SELinux is enabled: {selinux.security_getenforce() == 1}") print(f"Current SELinux mode: {'Enforcing' if selinux.security_getenforce() == 1 else 'Permissive'}") print(f"SELinux policy version: {selinux.security_getpolicyvers()}") else: print("SELinux is not enabled on this system or the module is not fully functional.")
Debug
Known issues
breakingVersion 0.3.0 changed the minimum Python requirement to 3.9. Projects using Python 3.8 or older will encounter installation errors or runtime issues.
fix
Upgrade your Python environment to 3.9 or newer, or pin your `selinux` dependency to `<0.3.0` if maintaining an older Python version.
affects: >=0.3.0
gotchaThis `selinux` package from PyPI is a shim/wrapper for the system-level `_selinux` module. It does NOT install SELinux functionality or the necessary underlying C libraries (`libselinux`) or their Python bindings (`libselinux-python`). If these are not already present and configured on your operating system, the module's functionality will be severely limited or unavailable.
fix
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).
affects: all
gotchaMany SELinux operations, especially those that modify policy or contexts, require elevated privileges (e.g., root) or specific SELinux capabilities. Running such functions without the necessary permissions will result in `PermissionError` or other runtime exceptions.
fix
Run your Python script with appropriate privileges if it needs to perform privileged SELinux operations. Always follow the principle of least privilege.
affects: all
gotchaWhile v0.2.0 introduced changes to 'Avoid errors on non-selinux platforms', some functions might still raise errors or return misleading values if SELinux is disabled or the system's `_selinux` module is not correctly loaded. Always check `selinux.is_selinux_enabled()` first.
fix
Always explicitly check `selinux.is_selinux_enabled()` before attempting core SELinux operations. Implement robust error handling for functions that interact with SELinux.
affects: <0.2.0 and potentially all
Upgrade
Version history
0.3.0latest on PyPI · released Dec 24, 2022
Audit
Dependencies

No dependency data recorded yet.

Agent activity
16 hits · last 30 days
node
12
OpenAI (training)
1
Resources