0

我正在运行一个 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

任何帮助将不胜感激,在此先感谢您。:)

4

0 回答 0