{"record":{"id":"b9a30da5f462e795","repo":"apache/beam","slug":"the-google-cloud-pubsub-library-is-required-for","errorCode":null,"errorMessage":"The 'google-cloud-pubsub' library is required for TestPubsubContext. Please install it using 'pip install google-cloud-pubsub'.","messagePattern":"The 'google-cloud-pubsub' library is required for TestPubsubContext\\. Please install it using 'pip install google-cloud-pubsub'\\.","errorType":"exception","errorClass":"ImportError","httpStatus":null,"severity":"error","filePath":"sdks/python/apache_beam/testing/pubsub_test_context.py","lineNumber":47,"sourceCode":"# pylint: enable=wrong-import-order, wrong-import-position\n\n\nclass TestPubsubContext:\n  \"\"\"A highly advanced Pub/Sub resource lifecycle manager for Python integration tests.\n    Implements cascading third-party subscription cleanup and selective\n    graceful teardown for debugging on failures.\n\n    Includes a safety 'dry_run' switch for safe deployment and validation of resources.\n    Any catastrophic leaks are handled independently by the global 'stale_cleaner.py'.\n    \"\"\"\n  def __init__(\n      self,\n      project_id,\n      dry_run=False\n  ):  # Keep dry_run=False to allow actual deletions during testing\n\n    if pubsub_v1 is None:\n      raise ImportError(\n          \"The 'google-cloud-pubsub' library is required for TestPubsubContext. \"\n          \"Please install it using 'pip install google-cloud-pubsub'.\")\n\n    self.project_id = project_id\n    self.dry_run = dry_run\n    self.publisher = pubsub_v1.PublisherClient()\n    self.subscriber = pubsub_v1.SubscriberClient()\n\n    # Lists to track resources created during the test execution\n    self.tracked_topics = []\n    self.tracked_subscriptions = []\n    self.caller_class = \"UnknownTestClass\"\n    stack = inspect.stack()\n\n    for frame in stack:\n      self_obj = frame[0].f_locals.get('self', None)\n      if self_obj and hasattr(self_obj, '__class__'):\n        self.caller_class = self_obj.__class__.__name__","sourceCodeStart":29,"sourceCodeEnd":65,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/python/apache_beam/testing/pubsub_test_context.py#L29-L65","documentation":"TestPubsubContext depends on the optional google-cloud-pubsub package; the module imports it defensively with pubsub_v1 possibly None. If the import failed, __init__ raises ImportError telling you to pip install google-cloud-pubsub. This keeps pubsub testing helpers usable in environments that lack the dependency.","triggerScenarios":"Instantiating TestPubsubContext in an environment where google-cloud-pubsub is not installed (or an incompatible installed version broke the import).","commonSituations":"Running Beam tests in a slim venv or CI image without extra pubsub extras; dependency removed during environment rebuild; version conflict hiding the package.","solutions":["pip install google-cloud-pubsub","Install Beam's test/gcp extras if applicable (e.g. pip install apache-beam[gcp])","Verify the import works: python -c 'import google.cloud.pubsub_v1'"],"exampleFix":"# before (ImportError at runtime)\nctx = TestPubsubContext(project_id='my-project')\n// after\n# $ pip install google-cloud-pubsub\nctx = TestPubsubContext(project_id='my-project')","handlingStrategy":"validation","validationCode":"try:\n    import google.cloud.pubsub_v1  # noqa\nexcept ImportError:\n    raise SystemExit(\"Install with: pip install google-cloud-pubsub\")","typeGuard":null,"tryCatchPattern":"try:\n    ctx = TestPubsubContext(project_id=project)\nexcept ImportError as e:\n    _LOGGER.error('%s — install the extra and rerun', e)\n    raise","preventionTips":["Add google-cloud-pubsub to test requirements / CI images","Use apache-beam[gcp] extras for Beam test environments","Smoke-test imports in setup scripts before running tests"],"tags":["dependency","pubsub","import-error","python"],"backgroundTag":"missing-optional-dependency","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-20T03:17:13.778Z"}