Install & Compatibility
Where this runs
tested against v1.6.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
py 3.10
✕ build_error
✓ 83.6s
py 3.11
✕ build_error
✓ 75.6s
py 3.12
✕ build_error
✓ 64.6s
py 3.13
✕ build_error
✓ 60.7s
py 3.9
✕ build_error
✕ timeout
4992MB installed
● package 4992MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
PrivacyEngine
✓ from opacus import PrivacyEngine
✗ from opacus.privacy_engine import PrivacyEngine
PrivacyEngine is exported from top-level opacus package
DPOptimizer
✓ from opacus.optimizers import DPOptimizer
✗ from opacus import DPOptimizer
DPOptimizer is in opacus.optimizers, not top-level
GradSampleModule
✓ from opacus import GradSampleModule
✗ from opacus.grad_sample import GradSampleModule
GradSampleModule is exported from top-level opacus
PRVAccountant
✓ from opacus.accountants import PRVAccountant
✗ from opacus import PRVAccountant
PRVAccountant is in opacus.accountants, not top-level
RDPAccountant
✓ from opacus.accountants import RDPAccountant
✗ from opacus import RDPAccountant
RDPAccountant is in opacus.accountants
Minimal DP training loop with Opacus using PrivacyEngine.make_private.
import torch
from torch import nn
from torch.utils.data import DataLoader, TensorDataset
from opacus import PrivacyEngine
# Create a simple model
torch.manual_seed(0)
model = nn.Linear(10, 2)
data = torch.randn(64, 10)
labels = torch.randint(0, 2, (64,))
train_dataset = TensorDataset(data, labels)
train_loader = DataLoader(train_dataset, batch_size=32)
# Define optimizer and loss
optimizer = torch.optim.SGD(model.parameters(), lr=0.01)
criterion = nn.CrossEntropyLoss()
# Attach privacy engine
privacy_engine = PrivacyEngine()
model, optimizer, train_loader = privacy_engine.make_private(
module=model,
optimizer=optimizer,
data_loader=train_loader,
noise_multiplier=0.5,
max_grad_norm=1.0,
)
# Training loop
for epoch in range(2):
for x, y in train_loader:
outputs = model(x)
loss = criterion(outputs, y)
loss.backward()
optimizer.step()
optimizer.zero_grad()
# Get privacy spent
epsilon = privacy_engine.get_epsilon(delta=1e-5)
print(f"Privacy spent: epsilon = {epsilon:.2f}")
Errors
Common errors & fixes
AttributeError: 'PrivacyEngine' object has no attribute 'make_private'
Using an older version of Opacus (pre-1.0) where the method was named differently.
fixUpgrade to latest Opacus: pip install --upgrade opacus
TypeError: cannot unpack non-iterable PrivacyEngine object
Unpacking make_private() result into two variables but it returns three (model, optimizer, data_loader) since v1.5.
fixChange to: model, optimizer, data_loader = privacy_engine.make_private(...)
RuntimeError: DataLoader worker process exited before finishing
Using the original data_loader instead of the one returned by make_private. Opacus replaces the sampler.
fixAlways use the data_loader returned by privacy_engine.make_private(). Never use the original DataLoader directly.
ModuleNotFoundError: No module named 'opt_einsum'
Opacus 1.5.0 and earlier required opt_einsum for linear layers; removed in 1.5.1+ but old installs may still need it.
fixUpgrade to opacus>=1.5.1 or install opt_einsum: pip install opt_einsum
Upgrade
Version history
1.6.0latest on PyPI · released May 5, 2026
Audit
Dependencies
torchrequiredOpacus requires PyTorch >=1.13