Google Cloud SDK importError: no module named clou

2019-07-25 08:05发布

问题:

I am new to Linux and trying to run a Python script that needs the following: 'from google.cloud import pubsub'

I'm getting the following error:

Traceback (most recent call last):
  File "file.py", line 2, in <module>
    from google.cloud import pubsub
ImportError: No module named google.cloud

How can I give access to this module? I have installed Google's Cloud SDK. I assume it has something to do with providing the path to this SDK "module" in some file?

回答1:

If this only happened when you deployed to app engine, then considering the following solution (it worked for me):

1, in the same directory of your app engine project (usually where you put your **.py, **.yaml, and index.html), add a python file named "appengine_config.py"

2, put the following code into your appengine_config.py:

# appengine_config.py
from google.appengine.ext import vendor

# Add any libraries install in the "lib" folder.
vendor.add('lib')

3, make sure on that directory you also has a "lib" folder that contains your library import (check if google.cloud is there)

4, deploy it and that problem should be resolved.



回答2:

If you want use pubsub in Python script, you should install via pip like this:

  • Install pip and virtualenv

    sudo apt-get install python-pip python-dev build-essential
    sudo pip install virtualenv
    
  • Create new environment and activate that

    cd project-folder
    virtualenv env
    source env/bin/activate
    
  • Install pubsub module

    pip install google-cloud-pubsub