redis/redis-py · error · ImportError
The PyJWT library is required for
Error message
The PyJWT library is required for {self.__class__.__name__}. What it means
Raised as ImportError inside JWToken.__init__ when `import jwt` fails, because the JWToken class decodes JWTs using the PyJWT library. PyJWT is an optional dependency (the jwt extra) and is not installed by default. The error names the class so you know which token implementation needs it.
Solutions
- Install PyJWT: `pip install PyJWT` (or `pip install redis[jwt]`).
- Add PyJWT (or the jwt extra) to your project's requirements/lockfile.
- If you did not intend to use JWT tokens, switch to a Token implementation that does not require PyJWT.
Example fix
# before: ImportError when constructing JWToken pip install redis # after pip install "redis[jwt]"
Defensive patterns
Strategy: validation
Validate before calling
try:
import jwt # noqa: F401
except ImportError:
raise SystemExit("PyJWT is required for JWT auth; pip install redis[jwt]")
token = JWToken(value) Type guard
def jwt_available() -> bool:
try:
import jwt # noqa: F401
return True
except ImportError:
return False Try / catch
try:
token = JWToken(value)
except ImportError as e:
if "PyJWT" in str(e):
raise SystemExit("Install PyJWT: pip install redis[jwt]")
raise Prevention
- Pin redis[jwt] (or PyJWT) in requirements when using JWT auth.
- Check dependency presence at deploy time, not at first auth.
When it happens
Trigger: Constructing a JWToken(token_string) instance (used by the auth/token_manager EntraID/JWT flows) in an environment where PyJWT is not installed. This happens during credential-provider or token-based-auth initialization that uses JWToken.
Common situations: Installing redis-py without the [jwt] extra. Using EntraID/JWT auth features that depend on JWToken without adding PyJWT to requirements. A fresh virtualenv that only installed the base redis package.
Related errors
- OpenTelemetry API is not installed. Install it with: pip…
- OpenTelemetry is not installed. Install it with: pip…
- Unexpected token schema. Following fields are missing
- cryptography is not installed.
- {e}
AI-assisted analysis of redis/redis-py@6a6b581b48 (2026-08-10).
Data as JSON: /api/errors/ea811511ab5facac.
Report an issue: GitHub.
Appendix: source
Thrown at redis/auth/token.py:85
def get_value(self) -> str:
return self.value
def get_expires_at_ms(self) -> float:
return self.expires_at
def get_received_at_ms(self) -> float:
return self.received_at
class JWToken(TokenInterface):
REQUIRED_FIELDS = {"exp"}
def __init__(self, token: str):
try:
import jwt
except ImportError as ie:
raise ImportError(
f"The PyJWT library is required for {self.__class__.__name__}.",
) from ie
self._value = token
self._decoded = jwt.decode(
self._value,
options={"verify_signature": False},
algorithms=[jwt.get_unverified_header(self._value).get("alg")],
)
self._validate_token()
def is_expired(self) -> bool:
exp = self._decoded["exp"]
if exp == -1:
return False
return (
self._decoded["exp"] * 1000 <= datetime.now(timezone.utc).timestamp() * 1000
)View on GitHub (pinned to 6a6b581b48)