谷歌云库无法正常工作(ImportError)

时间:2017-09-10 19:33:21

标签: python-2.7 google-app-engine google-cloud-platform google-cloud-pubsub client-library

我正在尝试使用cloud pub / sub从主题中提取消息数据但每次说明时都会收到ImportError

ImportError: dynamic module does not define init function (init_psutil_linux)

这是我的环境信息:

  • App Engine标准环境
  • Python2.7

这是我的代码:

import logging
import json
# from datetime import datetime, timedelta
import time

# Imports the Google Cloud client library
from google.cloud import pubsub_v1

import webapp2


class PullMessageData(webapp2.RequestHandler):
    """Pull Pub/Sub Message Data."""

    def get(self):
        self.response.headers['Content-Type'] = 'application/json'

        try:
            subscriber_name = 'subscriber_name'

            # pull subscription message
            pull_message_data(subscriber_name)
            self.response.write(json.dumps({'status': 'OK'}))

        except Exception, e:
            logging.exception(str(e))
            self.response.write(json.dumps({'status': 'ERROR'}))


def pull_message_data(subscriber_name):
    """pull message data from pubsub."""

    # instantiate the pubsub client for a default credential
    subscriber = pubsub_v1.SubscriberClient()

    # callback function for processing the Message Data
    def callback(message):           

        # process data
        print('Received message: {}'.format(message.data))

        # ack the message after processing
        message.ack()

    # Limit the subscriber to only have 5000 outstanding messages at a time.
    flow_control = pubsub_v1.types.FlowControl(max_messages=3)
    # pull message
    subscriber.subscribe(
        subscriber_name, callback=callback, flow_control=flow_control)

    # The subscriber is non-blocking, so we must keep the main thread from
    # exiting to allow it to process messages in the background.
    print('Listening for messages on {}'.format(subscriber_name))
    while True:
        time.sleep(10)

我按照the google cloud pubsub documentation page的文档进行操作 但是在我部署之后,我不断得到一个ImportError,它说:

(/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py:263)
Traceback (most recent call last):
  File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 240, in Handle
    handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
  File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 299, in _LoadHandler
    handler, path, err = LoadObject(self._handler)
  File "/base/data/home/runtimes/python27_experiment/python27_lib/versions/1/google/appengine/runtime/wsgi.py", line 85, in LoadObject
    obj = __import__(path[0])
  File "/base/data/home/apps/b~ups/1.404014226174083542/main.py", line 4, in <module>
    import pull_data
  File "/base/data/home/apps/b~ups/1.404014226174083542/pull_data.py", line 9, in <module>
    from google.cloud import pubsub_v1
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/google/cloud/pubsub_v1/__init__.py", line 17, in <module>
    from google.cloud.pubsub_v1 import types
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/google/cloud/pubsub_v1/types.py", line 20, in <module>
    import psutil
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/psutil/__init__.py", line 91, in <module>
    from . import _pslinux as _psplatform
  File "/base/data/home/apps/b~ups/1.404014226174083542/libs/psutil/_pslinux.py", line 26, in <module>
    from . import _psutil_linux as cext
ImportError: dynamic module does not define init function (init_psutil_linux)

这是我从以下地方调用文件的地方:

import os
import webapp2
import pull_data


app = webapp2.WSGIApplication([
    ('/pull', pull_data.PullMessageData),
], debug=True)

请有人帮忙解决有什么问题.....

0 个答案:

没有答案