我正在运行一个django站点,我遇到了在我的视图文件中运行的Http模块的问题。它在python虚拟环境中运行,我已经检查过以确保通过pip3 freeze命令安装了所有必需的软件包。 该模块将导入并运行在同一环境中的另一个脚本文件中,而不是我的views.py文件。尝试在views.py文件中导入模块时,出现以下错误:
[:error] [pid 2122] [remote 108.214.118.85:188] AttributeError: 'module' object has no attribute 'Http'
脚本上正常运行的代码如下:
import json
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
scopes = ['https://www.googleapis.com/auth/calendar']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes)
http_auth = credentials.authorize(Http())
caladmin = build('calendar', 'v3', http=http_auth)
events_query = caladmin.events().list(calendarId='calendarId').execute()
def get_events():
if not events_query['items']:
print('No upcoming events')
events = events_query['items']
print(events)
return events
get_events()
以下是返回上述错误的代码:
import os
import json
from oauth2client.service_account import ServiceAccountCredentials
from httplib2 import Http
from apiclient.discovery import build
from django.shortcuts import render
def news(request):
events = get_events()
context = {'events':events}
return render(request, 'district/about/news.html')
def get_events():
scopes = ['https://www.googleapis.com/auth/calendar']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes)
http_auth = credentials.authorize(Http())
caladmin = build('calendar', 'v3', http=http_auth)
events_query = caladmin.events().list(calendarId='calendarId').execute()
if not events_query['items']:
print('No upcoming events')
events = events_query['items']
return events
非常感谢任何帮助,谢谢你提前。 :)