[英]Google Cloud Platform - Datastore - Python ndb API
我最近開始使用 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 數據存儲區,但我無法弄清楚我做錯了什么。
推文.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
,我可以看到兩件事可能是您還不能在 Python 應用程序中使用tweepy的原因。 為了通過,我將記錄完整的過程。
由於Tweepy是第三方庫,而不是 Google App Engine 附帶的庫,因此我們必須將它與我們的 Python 應用程序一起打包,正如您已經通過pip install --target lib tweepy
建議的pip install --target lib tweepy
。 --target
選項后指定的目錄是第三方庫的下載目錄。
現在我們已經下載了第三方庫,在嘗試導入模塊時,必須讓 Python 搜索這個lib
目錄。 這可以按照您所展示的方式完成,使用應用程序目錄中的appengine_config.py
文件,如安裝庫中所示,如下所示:
from google.appengine.ext import vendor
vendor.add('lib')
Python 現在將在嘗試導入模塊時查看lib
目錄。 因此,我們在應用程序代碼中的適當位置添加我們的 import 語句(例如: tweet.py
):
import tweepy
此導入當前未在您的tweet.py
示例中找到。
請注意,導入時tweepy將嘗試導入ssl模塊,因此它必須包含在app.yaml
的libraries
部分中,如下所示:
libraries:
- name: ssl
version: latest
這也不是在您的示例app.yaml
。
有了上述內容,應該能夠成功部署一個正確導入tweepy的 python GAE 應用程序。 請注意,我沒有積極測試tweepy的任何功能,只是成功導入了它。 如果您在上述示例中看到任何錯誤,我建議您在問題中也包含以下內容:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.