{"id":"7a2e8b97126b590c","repo":"encode/httpx","slug":"using-http2-true-but-the-h2-package-is-not-inst","errorCode":null,"errorMessage":"Using http2=True, but the 'h2' package is not installed. Make sure to install httpx using `pip install httpx[http2]`.","messagePattern":"Using http2=True, but the 'h2' package is not installed\\. Make sure to install httpx using `pip install httpx\\[http2\\]`\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"httpx/_client.py","lineNumber":680,"sourceCode":"        super().__init__(\n            auth=auth,\n            params=params,\n            headers=headers,\n            cookies=cookies,\n            timeout=timeout,\n            follow_redirects=follow_redirects,\n            max_redirects=max_redirects,\n            event_hooks=event_hooks,\n            base_url=base_url,\n            trust_env=trust_env,\n            default_encoding=default_encoding,\n        )\n\n        if http2:\n            try:\n                import h2  # noqa\n            except ImportError:  # pragma: no cover\n                raise ImportError(\n                    \"Using http2=True, but the 'h2' package is not installed. \"\n                    \"Make sure to install httpx using `pip install httpx[http2]`.\"\n                ) from None\n\n        allow_env_proxies = trust_env and transport is None\n        proxy_map = self._get_proxy_map(proxy, allow_env_proxies)\n\n        self._transport = self._init_transport(\n            verify=verify,\n            cert=cert,\n            trust_env=trust_env,\n            http1=http1,\n            http2=http2,\n            limits=limits,\n            transport=transport,\n        )\n        self._mounts: dict[URLPattern, BaseTransport | None] = {\n            URLPattern(key): None","sourceCodeStart":662,"sourceCodeEnd":698,"githubUrl":"https://github.com/encode/httpx/blob/b5addb64f0161ff6bfe94c124ef76f6a1fba5254/httpx/_client.py#L662-L698","documentation":"Raised as ImportError in the sync Client.__init__ when http2=True but 'import h2' fails. HTTP/2 support is an optional extra; httpx refuses to construct an HTTP/2-capable client without the h2 package installed.","triggerScenarios":"Constructing httpx.Client(http2=True) in an environment where the h2 distribution is not installed (plain 'pip install httpx' without extras).","commonSituations":"Forgot the [http2] extra in requirements; CI image with minimal httpx; Dockerfile installing bare httpx; upgrading/rebuilding env and dropping the extra.","solutions":["Install the extra: pip install 'httpx[http2]'.","If HTTP/2 is not actually required, construct the client with http2=False (the default).","Pin both httpx and h2 (plus hyperframe, hpack) in requirements to keep them in sync."],"exampleFix":"// before\nhttpx.Client(http2=True)  # ImportError\n// after\n# pip install 'httpx[http2]'\nhttpx.Client(http2=True)","handlingStrategy":"validation","validationCode":"import importlib.util, httpx\nhttp2_available = importlib.util.find_spec(\"h2\") is not None\nif http2 and not http2_available:\n    raise RuntimeError(\"http2=True requires h2; run: pip install 'httpx[http2]'\")\nclient = httpx.Client(http2=http2 and http2_available)","typeGuard":"import importlib.util\n\ndef http2_ready() -> bool:\n    return importlib.util.find_spec(\"h2\") is not None","tryCatchPattern":"try:\n    client = httpx.Client(http2=True)\nexcept ImportError:\n    client = httpx.Client(http2=False)  # graceful fallback to HTTP/1.1","preventionTips":["Install httpx with the [http2] extra whenever you set http2=True.","Feature-detect h2 at startup and disable http2 if absent.","Pin h2, hyperframe and hpack in requirements alongside httpx."],"tags":["http2","config","dependency"],"analyzedSha":"b5addb64f0161ff6bfe94c124ef76f6a1fba5254","analyzedAt":"2026-08-04T19:32:56.768Z","schemaVersion":2}