找不到Python类导入属性

时间:2015-04-01 18:07:06

标签: python class memcached

我是python的新手,我试图创建一个非常简单的REST服务API调用。我需要这些数据才能将它导入另一个只支持python的应用程序。我的问题是我创建了一个python类并尝试将其导入我的应用程序。我有几个错误,然后修改我的类文件,包括添加一个名为parse_json的新函数。当我运行我的应用程序时,它说该对象没有属性parse_json。当我直接在类中运行代码时它可以工作,但是从一个单独的文件运行并导入该类会给我上面的错误。

../ bin / lib / meetupapi.py - >我的班级文件

class MeetupAPI(object):

    BASE_URL = 'https://api.meetup.com'

    def __init__(self, request_uri, api_key, group_urlname):
        self.request_uri = request_uri
        self.api_key = api_key
        self.group_urlname = group_urlname


    # Parse JSON query from API Call
    def parse_json(self, url, result_type=0):
        self.url = url
        self.result_type = result_type

        self.json_obj = urllib2.urlopen(self.url)
        self.data = json.load(self.json_obj)
        if result_type is not 0:
            if self.data[result_type]:
                return json.dumps(self.data[result_type])
        else:
            return json.dumps(self.data)


    # Get member IDs for profile
    def get_mid(self, request_type):
        self.request_type = request_type

        if self.request_type == 'member':
            self.url = MeetupAPI.BASE_URL + '/2/members?key=' + \
                self.api_key + '&group_urlname=' + self.group_urlname
        elif self.request_type == 'event':
            url = MeetupAPI.BASE_URL + '/2/events?key=' + \
                self.api_key + '&group_urlname=' + self.group_urlname

        self.json_obj = urllib2.urlopen(self.url)
        self.data = json.load(self.json_obj)
        self.mids = []

        for self.item in self.data['results']:
            if self.item['id']:
                self.mids.extend([self.item['id']])

        return self.mids

    # Get Group IDs for reference
    def get_gid(self):
        self.url = MeetupAPI.BASE_URL + '/' + \
            self.group_urlname + '?key=' + self.api_key

        return self.url

    # Main Meetup API Query for group based information
    def query(self):
        self.url = MeetupAPI.BASE_URL + self.request_uri + '?key=' + \
            self.api_key + '&group_urlname=' + self.group_urlname

        return self.url

    # Get member or event profile information
    def profile(self,mid,gid):
        self.mid = mid
        self.gid = gid

        self.url = MeetupAPI.BASE_URL + '/2/profile/' + \
            str(self.gid) + '/' + \
            str(self.mid) + '?key=' + self.api_key

        return self.url

../ bin / test.py - >我的测试文件

# Import required libraries
from meetupapi import MeetupAPI

Meetup = MeetupAPI(
    '/2/members',
    'xxxxxxx',
    'xxxxxxx'
)
mids = Meetup.get_mid('member')
group = Meetup.get_gid()
gid = Meetup.parse_json(group, 'id')

for mid in mids:
    member = Meetup.profile(mid, gid)
    profile = Meetup.parse_json(member)
    print profile

运行时出错

Traceback (most recent call last):
  File "./test2.py", line 25, in <module>
    gid = Meetup.parse_json(group, 'id')
AttributeError: 'MeetupAPI' object has no attribute 'parse_json'

请记住,我以前从未在python中写过一个类,所以我可能错过了一些非常简单的东西。我已经查看了为我能找到的其他人提供的所有答案,但似乎没有任何内容。非常感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:0)

检查lib目录中的meetupapi.pyc(注意“c”)并删除它。然后再次尝试导入模块。导入时,模块被编译为字节码。如果你修改了文件,新的编译应该自动完成,但可能没有。