如何修复AttributeError:';模块';对象没有属性';客户';在Google Cloud Interactive Shell中运行python时

如何修复AttributeError:';模块';对象没有属性';客户';在Google Cloud Interactive Shell中运行python时,python,python-2.7,google-cloud-platform,google-cloud-pubsub,google-cloud-shell,Python,Python 2.7,Google Cloud Platform,Google Cloud Pubsub,Google Cloud Shell,我正在尝试运行一个python脚本,模拟交通传感器在我的Google Cloud Shell上实时向PubSub发送数据。我犯了这个错误 Traceback (most recent call last): File "./send_sensor_data.py", line 87, in <module> psclient = pubsub.Client() AttributeError: 'module' object has no attribute 'Client

我正在尝试运行一个python脚本,模拟交通传感器在我的Google Cloud Shell上实时向PubSub发送数据。我犯了这个错误

Traceback (most recent call last):
  File "./send_sensor_data.py", line 87, in <module>
    psclient = pubsub.Client()
AttributeError: 'module' object has no attribute 'Client'

没有
pubsub.Client
类。你 需要选择
发布客户端
订阅客户端


请参见

pubsub.Client类在0.27.0版本的pubsub python包之前一直存在。所以我刚刚创建了一个虚拟环境,并在其中安装了pubsub的0.27.0版本。 以下是命令:

virtualenv venv
source venv/bin/activate
pip install google-cloud-pubsub==0.27.0

谷歌云平台的解决方案是:

  • 修改send_senor_data.py文件,如下所示:

    a。注释pub_sub的原始导入语句并使用_v1版本

    b。找到此代码并按如下方式替换:

  • 然后执行send_sensor_data.py,如下所示: ./send_sensor_data.py--speedFactor=60--project=YOUR-project-NAME

  • virtualenv venv
    source venv/bin/activate
    pip install google-cloud-pubsub==0.27.0
    
      #from google.cloud import pubsub
       from google.cloud import pubsub_v1
    
      #publisher = pubsub.PublisherClient()
      publisher = pubsub_v1.PublisherClient()