【发布时间】:2017-01-22 02:32:26
【问题描述】:
我最近开始使用 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]
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]
index.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')
非常感谢您的帮助。我感谢任何帮助和解释我做错了什么。
【问题讨论】:
-
您的模型定义中有错字:
nbd用于用户名。在部署之前,您应该真正在本地运行代码以查看任何错误。 -
感谢丹尼尔,感谢您的支持。我现在在本地运行我的应用程序。它确实帮助我找出了我最初的几个错误。不幸的是,我在 Google 上找不到任何关于如何在 python 代码中导入第三方库的信息。我添加了 appengine_config.py 但很难将 tweepy 导入到 tweet.py 中。
-
重新标记 - 这更像是一个 App Engine 问题,而不是 Cloud Datastore 问题,因为它是关于 App Engine 的代码/库部署。
标签: python google-app-engine google-cloud-platform google-cloud-datastore