Google-cloud-python: [pubsub] 'module' object has no attribute 'Client'

Created on 31 May 2017  路  20Comments  路  Source: googleapis/google-cloud-python

I have been using pubsub for a while but after updating it, I am getting
AttributeError: 'module' object has no attribute 'Client'

I am following this example from the official docs:

macOS 10.12.5 (16F73)

Name: google-cloud-pubsub
Version: 0.25.0
Summary: Python Client for Google Cloud Pub/Sub
Home-page: https://github.com/GoogleCloudPlatform/google-cloud-python
Author: Google Cloud Platform
Author-email: [email protected]
License: Apache 2.0
Requires: grpcio, gapic-google-cloud-pubsub-v1, google-cloud-core

Python 2.7.13 (default, Dec 18 2016, 07:03:39)
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import pubsub
>>> pubsub.Client()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Client'

ubuntu 16.04.2 (4.4.0-1013-aws)

Name: google-cloud-pubsub
Version: 0.25.0
Summary: Python Client for Google Cloud Pub/Sub
Home-page: https://github.com/GoogleCloudPlatform/google-cloud-python
Author: Google Cloud Platform
Author-email: [email protected]
License: Apache 2.0
Location: /usr/local/lib/python2.7/dist-packages
Requires: google-cloud-core, gapic-google-cloud-pubsub-v1, grpcio

Python 2.7.12 (default, Nov 19 2016, 06:48:10)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import pubsub
>>> pubsub.Client()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'Client'

I have already uninstalled and reinstalled it (and the entire dependency tree) many times.

packaging pubsub

Most helpful comment

Thanks @tseaver, reinstalling the packages (and downgrading google-cloud-pubsub to 0.27) did the trick. Apparently pubsub.Client was removed in 0.28

All 20 comments

@mwilliammyers We've seen similar issues before due to broken install states combined with namespace packages. It's often due to the presence of a .egg file or something similar.

Can you install into a virtualenv to make sure the issue is your environment (then we can try to dig into your /usr/local/lib/python2.7/dist-packages or the other Location on Mac OS X and find the bad file).

$ pip install --upgrade virtualenv
$ export VENV=venv  # or any other name you choose other than "venv"
$ virtualenv ${VENV}
$ source ${VENV}/bin/activate
$ ${VENV}/bin/pip install --upgrade google-cloud-pubsub
$ ${VENV}/bin/python -c "from google.cloud import pubsub; print(pubsub.Client)"

Thanks for the quick response!

That macOS post was within a virtualenv env.

@mwilliammyers Do you mind sharing the code you used to create the virtualenv? Would you mind trying the code I provided with a fresh virtualenv?

I got it working in my venv in ubuntu and macOS:

ubuntu@ip-172-31-5-242:/tmp$ virtualenv env
Running virtualenv with interpreter /usr/bin/python2
New python executable in /tmp/env/bin/python2
Also creating executable in /tmp/env/bin/python
Installing setuptools, pkg_resources, pip, wheel...done.
ubuntu@ip-172-31-5-242:/tmp$ source env/bin/activate



md5-881eb0ff8f7ca5cd7246771076ce770b



(env) ubuntu@ip-172-31-5-242:/tmp$ which pip
/tmp/env/bin/pip



md5-881eb0ff8f7ca5cd7246771076ce770b



(env) ubuntu@ip-172-31-5-242:/tmp$ pip install --upgrade google-cloud-pubsub



md5-881eb0ff8f7ca5cd7246771076ce770b



(env) ubuntu@ip-172-31-5-242:/tmp$ python -c "from google.cloud import pubsub; pubsub.Client()"

I did a similar thing in a new env on macOS and it works.

I think I tracked down the offending package (which I hadnt installed yet into the new envs): pyrebase

@mwilliammyers So can I close this issue out or are we still fishing for a cause? (Also, I highly recommend using a "direct path" to things in ${VENV}/bin rather than relying on PATH to "just work".)

Yes, thank you!

@mwilliammyers In addition to what @dhermes says, I would particularly avoid using activate (start from a fresh terminal / shell). activate leaves droppings in the environment which can cause oddities like the ones you have reported.

The ubuntu one was from a fresh ssh session, so it was a new shell. A lot of times I do an exec as well.

Glad to hear we could help @mwilliammyers

@dhermes I'm getting the same error, and am unsure how to debug this. I'm working in a virtual env, and have been digging in venv/lib/python2.7/site-packages, with no luck.

@apengwin Can you create a fresh virtualenv and post here all the commands you used to create/activate the virtualenv, then to import. For example, on my machine I just did:

$ virtualenv issue-3461
New python executable in .../issue-3461/bin/python2.7
Also creating executable in .../issue-3461/bin/python
Installing setuptools, pip, wheel...done.
$
$ source issue-3461/bin/activate
(issue-3461) $
(issue-3461) $ issue-3461/bin/pip install --upgrade google-cloud-pubsub
Collecting google-cloud-pubsub
  Downloading google_cloud_pubsub-0.25.0-py2.py3-none-any.whl
...
Successfully installed ...
(issue-3461) $
(issue-3461) $ issue-3461/bin/python
Python 2.7.13 (default, Jan  5 2017, 18:58:25)
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import pubsub
>>> pubsub.Client
<class 'google.cloud.pubsub.client.Client'>
>>>
(issue-3461) $ deactivate
$ rm -fr issue-3461/
$

@dhermes thanks for the prompt response! When I open everything in a fresh virtualenv it works fine.

airbears2-10-142-35-42:test allanpeng$ virtualenv issue-3461
New python executable in /Users/allanpeng/test/issue-3461/bin/python
Installing setuptools, pip, wheel...done.


airbears2-10-142-35-42:test allanpeng$ source issue-3461/bin/activate


(issue-3461) airbears2-10-142-35-42:test allanpeng$ issue-3461/bin/pip install --upgrade google-cloud-pubsub
Collecting google-cloud-pubsub
  Using cached google_cloud_pubsub-0.25.0-py2.py3-none-any.whl
...
Successfully installed ....

(issue-3461) airbears2-10-142-35-42:test allanpeng$ issue-3461/bin/python

Python 2.7.10 (default, Jul 30 2016, 19:40:32) 
[GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from google.cloud import pubsub
>>> pubsub.Client
<class 'google.cloud.pubsub.client.Client'>
>>> ^D


(issue-3461) airbears2-10-142-35-42:test allanpeng$ deactivate

airbears2-10-142-35-42:test allanpeng$ 

Good to hear. It seems like your other environment may be broken. Case closed?

Yeah. That seems to be the case. I'm not sure how I'd go about fixing the other environment though.

@apengwin Just delete the other environment :grinning:. If that's not an option, you can look around for bad pth files and also inspect __path__ and/or __file__ on modules that are behaving badly, e.g.

>>> import google
>>> google.__path__

If you have python aliased to ipython, try:

unalias python
python -c "from google.cloud import pubsub; pubsub.Client()"

Hi,

So I am facing exactly the same problem, except I don't think virtualenv would be an option in my case and I would rather not delete my current environment (too many CRON jobs depending on it). I am trying to fix my current environment so it works.

try:
    import dev_appserver
    dev_appserver.fix_sys_path()
except ImportError:`
    pass
from google.cloud import pubsub
print pubsub.__path__
c = pubsub.Client()

fails with

['/usr/local/lib/python2.7/dist-packages/google/cloud/pubsub']
Traceback (most recent call last):
File "test_pubsub.py", line 3, in <module>
c = pubsub.Client()
AttributeError: 'module' object has no attribute 'Client'

I also noticed that if I run the same script with IPython it finds pubsub.Client and the path is slightly different:

['/home/fcaldas/google-cloud-sdk/platform/google_appengine/google/cloud/pubsub',
'/usr/local/lib/python2.7/dist-packages/google/cloud/pubsub']

Any ideas on how to fix it from here?

I would double check that you are not being hosed by a pip / setuptools whack-a-mole bug due to namespace packages, recently fixed (again):

$ pip install -U setuptools pip

Thanks @tseaver, reinstalling the packages (and downgrading google-cloud-pubsub to 0.27) did the trick. Apparently pubsub.Client was removed in 0.28

I had the same problem with language.Client and figured out some methods are deprecated.
look here:
https://cloud.google.com/natural-language/docs/python-client-migration

Was this page helpful?
0 / 5 - 0 ratings