Skip to main content
 首页 » 编程设计

python之Google Cloud Platform之数据存储之Python ndb API

2024年10月17日6myhome

我最近开始使用 Google Cloud Platform,更准确地说,是使用 ndb-Datastore-API。我尝试使用以下教程 ( https://github.com/GoogleCloudPlatform/appengine-guestbook-python.git ) 来习惯 API。

不幸的是,我不知道如何将第三方库 tweepy 导入到 tweet.py 中。 Google Cloud 不支持 tweepy,因此我必须手动将库包含在文件夹/lib 中。但是我现在如何导入安装的 tweepy (pip install -t lib tweepy)?

我基本上只是尝试将一个实体放入 Google 数据存储区,但我无法弄清楚我做错了什么。

tweet.py:

    # [START imports] 
from __future__ import absolute_import, print_function 
import os 
import urllib 
from time import * 
import jinja2 
import webapp2 
from google.appengine.api import users 
from google.appengine.ext import ndb 
 
JINJA_ENVIRONMENT = jinja2.Environment( 
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)), 
    extensions=['jinja2.ext.autoescape'], 
    autoescape=True) 
# [END imports] 
 
# [START globalvar] 
# Go to http://apps.twitter.com and create an app. 
# The consumer key and secret will be generated for you after 
consumer_key="KEY" 
consumer_secret="SECRET" 
# After the step above, you will be redirected to your app's page. 
# Create an access token under the the "Your access token" section 
access_token="TOKEN" 
access_token_secret="SECRET" 
# [END globalvar] 
 
USERNAME = "@Seeed" 
 
def getDate(): 
    # local Time 
    lt = localtime() 
    # get Date 
    year, month, day = lt[0:3] 
    date = "%02i.%02i.%04i" % (day,month,year) 
    return date 
 
# [START tweet_count_entity] 
class TweetCount(ndb.Model): 
    """A main model for representing an individual TweetCount entry.""" 
    date = ndb.DateProperty(auto_now_add=True) 
    tweets = ndb.IntegerProperty(indexed=False) 
    user_name = ndb.StringProperty(indexed=False) 
 
# [END tweet_count_entity] 
 
# [START tweet_counter] 
class TweetCounter(webapp2.RequestHandler): 
    """ 
    # Create a key for the Entity 
    def tweetCount_key(date): 
        date = getDate() 
        return ndb.Key('TweetCount', date)""" 
 
    # Get TweetCount for actor "user_name" 
    def getTweetCount(self, user_name): 
        auth = OAuthHandler(consumer_key, consumer_secret) 
        auth.set_access_token(access_token, access_token_secret) 
        api = tweepy.API(auth) 
        user = api.get_user(user_name) 
        return user.followers_count 
 
    def get(self): 
        count = self.getTweetCount(USERNAME) 
        tweet_count_user_name = USERNAME 
        tweet_count_tweets = count 
        tweet_count = TweetCount(tweets=tweet_count_tweets, user_name=tweet_count_user_name) 
        tweet_count.put() 
        self.response.headers['Content-Type'] = 'text/plain' 
        self.response.write("" + USERNAME + " TweetCount: " + str(count)) 
 
# [END tweet_counter] 
 
# [START app] 
app = webapp2.WSGIApplication([ 
    ('/', TweetCounter), 
], debug=True) 
# [END app] 

应用程序.yaml:

runtime: python27 
api_version: 1 
threadsafe: true 
 
# [START handlers] 
handlers: 
- url: /.* 
  script: tweet.app 
# [END handlers] 
 
# [START libraries] 
libraries: 
- name: webapp2 
  version: latest 
- name: jinja2 
  version: latest 
# [END libraries] 

索引.yaml:

indexes: 
- kind: TweetCount 
  properties: 
  - name: date 
  - name: tweets 
  - name: user_name 

appengine_config.py:

from google.appengine.ext import vendor 
 
# Add any libraries installed in the "lib" folder. 
vendor.add('lib') 

非常感谢您的帮助。我感谢任何帮助和对我做错了什么的解释。

请您参考如下方法:

根据您的 tweet.py 和 app.yaml,我可以看出可能是您还不能使用 tweepy 在您的 Python 应用程序中。为了完整起见,我将记录完整的过程。

获取 tweepy 库

因为 Tweepy 是一个 third party library而不是 Google App Engine 附带的库,我们必须将它与我们的 Python 应用程序打包在一起,正如您已经在 pip install --target lib tweepy 中建议的那样. --target 选项后指定的目录是第三方库的下载位置。

第三方目录中的vendor

既然我们已经下载了第三方库,那么在尝试导入模块时,必须让 Python 搜索这个 lib 目录。这可以如您所展示的那样完成,使用应用程序目录中的 appengine_config.py 文件,如 Installing a library 中所示。像这样:

from google.appengine.ext import vendor 
vendor.add('lib') 

在应用中导入第三方模块

当尝试导入模块时,Python 现在将在 lib 目录中查找。因此,我们在应用程序代码中的适当位置添加导入语句(例如:tweet.py):

import tweepy 

当前在您的 tweet.py 示例中找不到此导入。

确保 tweepy 的依赖项也被导入

请注意,tweepy 在导入时将尝试导入 ssl 模块,因此它必须包含在 的 libraries 部分中app.yaml 像这样:

libraries: 
- name: ssl 
  version: latest 

这也不在您的示例 app.yaml 中。

通过以上内容,应该能够成功部署正确导入 tweepy 的 python GAE 应用程序。请注意,我没有主动测试 tweepy 的任何功能,而只是成功导入了它。如果您发现上述示例有任何错误,我建议您在问题中也包括以下内容:

  • App Engine SDK 版本
  • Python 版本
  • 点子版本
  • 您在部署或提供 HTTP 响应时收到的错误堆栈跟踪