我对 Python 和 Google App Engine 都很陌生,我正在慢慢地尝试解决以下编译错误。
File "/opt/google/google_appengine/google/appengine/tools/appengine_rpc.py", line 26, in import cookielib File "/usr/lib/python2.7/cookielib.py", line 38, in from calendar import timegm ImportError: cannot import name timegm
我在 Eclipse 中使用 PyDev 插件进行本地部署。据我所知,没有错误的原因。我已经尝试将 timegm.py 文件夹添加到 PYTHONPATH 配置中,我什至通过在我的代码中使用自动完成简单地导入 from calendar import timegm 来证明这一点!
我见过其他人遇到过这个问题,但没有解决方案。有人知道如何解决这个问题吗?
代码如下:
import httplib2
import webapp2
from apiclient.discovery import build
from google.appengine.api import oauth
from oauth2client.client import OAuth2WebServerFlow
from urlparse import urlparse, parse_qs
class MainPage(webapp2.RequestHandler):
def get(self):
flow = OAuth2WebServerFlow(__CLIENT_ID, __CLIENT_SECRET, _SCOPE, _REDIRECT_URI)
authUri = flow.step1_get_authorize_url()
queryString = parse_qs(urlparse(authUri).query)
if 'error' not in queryString:
# Create an httplib2.Http object to handle our HTTP requests and authorize it
credentials = flow.step2_exchange(queryString['code'])
http = httplib2.Http()
http = credentials.authorize(http)
service = build("calendar", "v3", http=http)
events = service.events().list(calendarId=__VISITORS_CALENDAR).execute(http=http)
if events['items']:
# show what we've got
for event in events['items']:
self.response.write(event['summary'])
else:
self.response.write('No events found in the calendar')
else:
self.response.write('Denied...')
app = webapp2.WSGIApplication([('/', MainPage)], debug = True)
请您参考如下方法:
无需安装任何东西,calendar.timegm 是标准库中的函数。
可能发生的情况是您有一个名为 calendar.py 的本地文件,它隐藏了 stdlib 版本。您的日历文件没有这样的功能,因此出现错误。将您的文件重命名为其他名称。
