google cloud pubsub ImportError:无法导入名称类型

时间:2018-07-03 04:27:08

标签: python-2.7 google-app-engine google-cloud-pubsub

我使用google-cloud-pubsub在python的标准环境下为Google appengine编写了一个小程序。我得到一个错误 ImportError:无法导入名称类型。我还看到problem仍然没有解决。但是也许有人在标准环境中成立了子酒吧?

我安装了lib:pip installall -t lib google-cloud-pubsub。

在appengine_config.py中:vendor.add('lib')

访问appengine应用程序时出错:

Traceback (most recent call last):
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/alloc/tmpfs/dynamic_runtimes/python27g/7894e0c59273b2b7/python27/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/e~pqcloud-sp/agg2:20180703t012034.410851540172441919/service_main.py", line 5, in <module>
    from google.cloud.pubsub_v1 import PublisherClient
  File "/base/data/home/apps/e~pqcloud-sp/agg2:20180703t012034.410851540172441919/lib/google/cloud/pubsub_v1/__init__.py", line 17, in <module>
    from google.cloud.pubsub_v1 import types
ImportError: cannot import name types

app.yaml:

runtime: python27
api_version: 1
threadsafe: true
service: agg2

handlers:
- url: .*
  script: service_main.app

libraries:
- name: webapp2
  version: "2.5.1"
- name: jinja2
  version: latest

skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$

service_main.py:

import os
import logging
import webapp2

from google.cloud.pubsub_v1 import PublisherClient

logger = logging.getLogger('service_main')
logger.setLevel(logging.WARNING)

class ServiceTaskMainHandler(webapp2.RequestHandler):
    def get(self):
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write('test')


config = {
    'webapp2_extras.sessions': {
        'secret_key': 'YOUR_SECRET_KEY'
    }
}

MAIN_ROUTE = [
    webapp2.Route('/', ServiceTaskMainHandler, name='main'),
]

app = webapp2.WSGIApplication(MAIN_ROUTE, debug=True, config=config)

tree lib / google / cloud / pubsub_v1

lib/google/cloud/pubsub_v1
├── exceptions.py
├── exceptions.pyc
├── futures.py
├── futures.pyc
├── gapic
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── publisher_client_config.py
│   ├── publisher_client_config.pyc
│   ├── publisher_client.py
│   ├── publisher_client.pyc
│   ├── subscriber_client_config.py
│   ├── subscriber_client_config.pyc
│   ├── subscriber_client.py
│   └── subscriber_client.pyc
├── _gapic.py
├── _gapic.pyc
├── __init__.py
├── __init__.pyc
├── proto
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── pubsub_pb2_grpc.py
│   ├── pubsub_pb2_grpc.pyc
│   ├── pubsub_pb2.py
│   └── pubsub_pb2.pyc
├── publisher
│   ├── batch
│   │   ├── base.py
│   │   ├── base.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── thread.py
│   │   └── thread.pyc
│   ├── client.py
│   ├── client.pyc
│   ├── exceptions.py
│   ├── exceptions.pyc
│   ├── futures.py
│   ├── futures.pyc
│   ├── __init__.py
│   └── __init__.pyc
├── subscriber
│   ├── client.py
│   ├── client.pyc
│   ├── futures.py
│   ├── futures.pyc
│   ├── __init__.py
│   ├── __init__.pyc
│   ├── message.py
│   ├── message.pyc
│   ├── _protocol
│   │   ├── bidi.py
│   │   ├── bidi.pyc
│   │   ├── dispatcher.py
│   │   ├── dispatcher.pyc
│   │   ├── heartbeater.py
│   │   ├── heartbeater.pyc
│   │   ├── helper_threads.py
│   │   ├── helper_threads.pyc
│   │   ├── histogram.py
│   │   ├── histogram.pyc
│   │   ├── __init__.py
│   │   ├── __init__.pyc
│   │   ├── leaser.py
│   │   ├── leaser.pyc
│   │   ├── requests.py
│   │   ├── requests.pyc
│   │   ├── streaming_pull_manager.py
│   │   └── streaming_pull_manager.pyc
│   ├── scheduler.py
│   └── scheduler.pyc
├── types.py
└── types.pyc

1 个答案:

答案 0 :(得分:1)

代码失败的原因是因为App Engine Standard的Python2.7运行时不支持发布/订阅云客户端库,仅支持发布/订阅API客户端库。 some new code samples展示了如何在App Engine Standard中使用发布/订阅。

import googleapiclient.discovery
import base64

service = build('pubsub', 'v1')

topic_path = 'projects/{your_project_id}/topics/{your_topic}'

service.projects().topics().publish(
    topic=topic_path, body={
      "messages": [{
          "data": base64.b64encode(data)
      }]
    }).execute()

更新:GAE(Google App Engine)标准版和GAE Flexible Python 3 Runtime均支持Cloud Pub / Sub客户端库。