ImportError: cannot import name 'pubsub' from 'google.cloud'
Fixes the google.cloud namespace failing to expose pubsub after installing google-cloud-pubsub. Use when from google.cloud import pubsub fails. Not for credential errors.
TL;DR: The google.cloud namespace is shadowed, almost always by the retired google-cloud meta-package. Uninstall google-cloud, force-reinstall google-cloud-pubsub, and the import works.
ImportError: cannot import name 'pubsub' from 'google.cloud'Fix it
- Remove the legacy meta-package: pip uninstall google-cloud. Expected: gone.
- Reinstall: pip install --force-reinstall google-cloud-pubsub. Expected: clean install.
- Verify: python -c "from google.cloud import pubsubv1; print('ok')". Expected: ok. (pubsubv1 is the canonical import; the pubsub alias usually works too once the namespace is fixed.)
- Check interpreter alignment with python -m pip if it still fails.
When this applies
- from google.cloud import pubsub fails but pip shows google-cloud-pubsub.
When it doesn't
- pip lacks google-cloud-pubsub: install it.
- Publisher calls 403: IAM permissions, not this.
Compatibility
- google-cloud-pubsub >= 1.0; Python 3.8+.
Why it happens
All google-cloud-* packages share the google.cloud namespace. One legacy package with a real init.py breaks resolution for every submodule.
Edge cases
- The same failure hits bigquery, storage, and every other google-cloud-* package; the fix is identical.
Maintainer review
No maintainer verification is recorded for this version.
This records the version a maintainer checked. It does not assert that the version is the latest upstream release.