{"id":"ea811511ab5facac","repo":"redis/redis-py","slug":"the-pyjwt-library-is-required-for-self-class","errorCode":null,"errorMessage":"The PyJWT library is required for {self.__class__.__name__}.","messagePattern":"The PyJWT library is required for (.+?)\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"redis/auth/token.py","lineNumber":85,"sourceCode":"\n    def get_value(self) -> str:\n        return self.value\n\n    def get_expires_at_ms(self) -> float:\n        return self.expires_at\n\n    def get_received_at_ms(self) -> float:\n        return self.received_at\n\n\nclass JWToken(TokenInterface):\n    REQUIRED_FIELDS = {\"exp\"}\n\n    def __init__(self, token: str):\n        try:\n            import jwt\n        except ImportError as ie:\n            raise ImportError(\n                f\"The PyJWT library is required for {self.__class__.__name__}.\",\n            ) from ie\n        self._value = token\n        self._decoded = jwt.decode(\n            self._value,\n            options={\"verify_signature\": False},\n            algorithms=[jwt.get_unverified_header(self._value).get(\"alg\")],\n        )\n        self._validate_token()\n\n    def is_expired(self) -> bool:\n        exp = self._decoded[\"exp\"]\n        if exp == -1:\n            return False\n\n        return (\n            self._decoded[\"exp\"] * 1000 <= datetime.now(timezone.utc).timestamp() * 1000\n        )","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/redis/redis-py/blob/da03cdc7e8731092b13e395605c3c1fb2de25de1/redis/auth/token.py#L67-L103","documentation":"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.","triggerScenarios":"Constructing JWToken (directly or via an auth/token_manager flow that uses JWT tokens) in an environment where the PyJWT package is not installed.","commonSituations":"Installing redis-py without the jwt extra; using EntraID/token auth features that require PyJWT; fresh virtualenv missing optional deps.","solutions":["Install PyJWT: `pip install PyJWT` (or `pip install redis[jwt]`).","Pin a compatible PyJWT version in your requirements file.","If you do not need JWT decoding, use SimpleToken instead of JWToken."],"exampleFix":"# before\n# pip install redis   (jwt extra missing)\nfrom redis.auth.token import JWToken\nt = JWToken(raw_jwt)  # ImportError\n# after\n# pip install redis[jwt]\nfrom redis.auth.token import JWToken\nt = JWToken(raw_jwt)","handlingStrategy":"validation","validationCode":"try:\n    import jwt  # noqa: F401\n    HAVE_JWT = True\nexcept ImportError:\n    HAVE_JWT = False\nif not HAVE_JWT:\n    raise EnvironmentError('Install PyJWT: pip install redis[jwt]')","typeGuard":"def jwt_available() -> bool:\n    try:\n        import jwt  # noqa: F401\n        return True\n    except ImportError:\n        return False","tryCatchPattern":"try:\n    from redis.auth.token import JWToken\n    token = JWToken(raw)\nexcept ImportError as e:\n    raise RuntimeError('PyJWT missing') from e","preventionTips":["Install the jwt extra wherever JWT auth is used.","Pin PyJWT in requirements to avoid silent removal."],"tags":["dependencies","auth","jwt"],"analyzedSha":"da03cdc7e8731092b13e395605c3c1fb2de25de1","analyzedAt":"2026-08-04T20:26:47.563Z","schemaVersion":2}