Install & Compatibility
Where this runs
tested against v1.43.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
muslpy 3.10–3.95 runs
installs and imports cleanly · install 0.0s · import 0.124s · 21.4MB
glibcpy 3.10–3.95 runs
installs and imports cleanly · install 1.8s · import 0.114s · 22MB
20MB installed
● package 20MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
Enforcer
✓ from casbin import Enforcer
This quickstart demonstrates how to initialize a Casbin Enforcer with a basic model and policy, and then use it to check authorization requests. It creates temporary `model.conf` and `policy.csv` files, which define the access control structure and rules respectively. The `enforce` method is then called to determine if a subject (user), object (resource), and action combination is allowed.
import casbin
import os
# Create a simple model.conf file
model_conf_content = """
[request_definition]
r = sub, obj, act
[policy_definition]
p = sub, obj, act
[policy_effect]
e = some(where (p.eft == allow))
[matchers]
m = r.sub == p.sub && r.obj == p.obj && r.act == p.act
"""
# Create a simple policy.csv file
policy_csv_content = """
p, alice, data1, read
p, bob, data2, write
"""
# Save model and policy to temporary files
with open("model.conf", "w") as f:
f.write(model_conf_content)
with open("policy.csv", "w") as f:
f.write(policy_csv_content)
try:
# Initialize the enforcer
e = casbin.Enforcer("model.conf", "policy.csv")
# Test enforcement
print(f"Alice can read data1: {e.enforce('alice', 'data1', 'read')}") # True
print(f"Alice can write data1: {e.enforce('alice', 'data1', 'write')}") # False
print(f"Bob can read data2: {e.enforce('bob', 'data2', 'read')}") # False
print(f"Bob can write data2: {e.enforce('bob', 'data2', 'write')}") # True
print(f"Charlie can read data1: {e.enforce('charlie', 'data1', 'read')}") # False
finally:
# Clean up temporary files
os.remove("model.conf")
os.remove("policy.csv")
Debug
Known issues
breakingWhen upgrading to PyCasbin v2 (which was released with version 0.20.0), custom effectors require a rewrite due to API changes.fixReview the Casbin documentation and changelog for PyCasbin v2 regarding custom effector implementations and update your code accordingly.
affects: 0.x.x to 2.x.x (specifically 0.20.0 and above)
gotchaThe core `casbin` library only includes a default file adapter. For policy persistence in databases (e.g., MySQL, PostgreSQL, MongoDB), you must install a separate, corresponding adapter library.fixInstall the appropriate adapter package, e.g., `pip install casbin-sqlalchemy-adapter` or `pip install casbin-pymongo-adapter`, and configure the `Enforcer` to use it.
affects: All versions
gotchaCasbin handles *authorization* (who can do what on which resource) but explicitly *does not* handle authentication (verifying user identity/passwords).fixImplement a separate authentication mechanism (e.g., OAuth, JWT, session management) in your application to verify user identities before passing them to Casbin for authorization checks.
affects: All versions
gotchaIn distributed systems, the `SyncEnforcer`'s periodic policy reloading might lead to temporary inconsistencies or frequent database hits. Consider using filtered policy loading or a robust caching strategy.fixFor large policy sets or distributed environments, explore Casbin's filtered policy loading feature or implement a separate caching layer with explicit invalidation to reduce database pressure and ensure policy freshness.
affects: All versions, especially in high-load or distributed environments
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'casbin'
The 'casbin' library is not installed in the current Python environment or the environment where the code is being executed.
fixInstall the 'casbin' package using pip: `pip install casbin`
AttributeError: 'Model' object has no attribute 'add_policy'
Policy management methods like `add_policy`, `remove_policy`, etc., are intended to be called on an initialized `casbin.Enforcer` object, not directly on the `Model` object.
fixEnsure you are calling policy management methods on an instance of `casbin.Enforcer` after it has been created: `enforcer.add_policy('alice', 'data1', 'read')` FileNotFoundError: [Errno 2] No such file or directory: '<filepath>'
The `casbin.Enforcer` constructor or its internal methods (`LoadModel`, `LoadPolicy`) cannot find the specified model (`.conf`) or policy (`.csv`) file at the given path.
fixVerify that the paths provided to the `casbin.Enforcer` constructor for the model and policy files are correct and that the files exist and are accessible in the specified location.
AttributeError: '<class_instance>' object has no attribute '<attribute_name>'
When using Attribute-Based Access Control (ABAC), your Casbin model's matcher (e.g., `m = r.obj.Owner == p.obj.Owner`) references an attribute (e.g., 'Owner') that is not present in the Python object passed as an argument (e.g., `obj`) to the `enforce()` method.
fixEnsure that the Python object (e.g., `sub`, `obj`, `act`) passed to `enforcer.enforce()` is an instance of a class or has attributes that exactly match those referenced in your ABAC model's matcher definition.
Upgrade
Version history
1.43.0latest on PyPI · released May 10, 2025
Audit
Dependencies
No dependency data recorded yet.