redis/redis-py · error · ImportError

The PyJWT library is required for {self.__class__.__name__}.

Error message

The PyJWT library is required for {self.__class__.__name__}.

What it means

Raised as ImportError by JWToken.__init__ (redis/auth/token.py:85) when `import jwt` fails. The JWToken class decodes JWTs with PyJWT, which is an optional dependency not installed by default. The message names the class so you know which feature needs it.

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 da03cdc7e8)

Solutions

  1. Install PyJWT: `pip install PyJWT` (or `pip install redis[jwt]`).
  2. Pin a compatible PyJWT version in your requirements file.
  3. If you do not need JWT decoding, use SimpleToken instead of JWToken.

Example fix

# before
# pip install redis   (jwt extra missing)
from redis.auth.token import JWToken
t = JWToken(raw_jwt)  # ImportError
# after
# pip install redis[jwt]
from redis.auth.token import JWToken
t = JWToken(raw_jwt)
Defensive patterns

Strategy: validation

Validate before calling

try:
    import jwt  # noqa: F401
    HAVE_JWT = True
except ImportError:
    HAVE_JWT = False
if not HAVE_JWT:
    raise EnvironmentError('Install PyJWT: pip install redis[jwt]')

Type guard

def jwt_available() -> bool:
    try:
        import jwt  # noqa: F401
        return True
    except ImportError:
        return False

Try / catch

try:
    from redis.auth.token import JWToken
    token = JWToken(raw)
except ImportError as e:
    raise RuntimeError('PyJWT missing') from e

Prevention

When it happens

Trigger: Constructing JWToken (directly or via an auth/token_manager flow that uses JWT tokens) in an environment where the PyJWT package is not installed.

Common situations: Installing redis-py without the jwt extra; using EntraID/token auth features that require PyJWT; fresh virtualenv missing optional deps.

Related errors


AI-assisted analysis of redis/redis-py@da03cdc7e8 (2026-08-04). Data as JSON: /data/errors/ea811511ab5facac.json. Report an issue: GitHub.