Python 3.5.1:NameError:name' json'没有定义

时间:2016-12-06 18:04:49

标签: python python-3.5

#!/usr/bin/env python
# encoding: utf-8

import tweepy #https://github.com/tweepy/tweepy
import json as simplejson


    #write tweet objects to JSON
    file = open('tweet.json', 'wb') 
    print ("Writing tweet objects to JSON please wait...")
    for status in alltweets:
        json.dump(status._json,file,sort_keys = True,indent = 4)

    #close the file
    print ("Done")
    file.close()

if __name__ == '__main__':
    #pass in the username of the account you want to download
    get_all_tweets("@AlertZaAfrica")

python编译器说第54行是错误的。我已经将import json定义为simplejson。我定义导入json的上述区域如上所示。

1 个答案:

答案 0 :(得分:15)

您应该首先将simplejson安装到您的系统中以便能够导入它:

$ sudo pip3 install simplejson

然后在您的代码中,您现在可以导入它:

import simplejson as json

从现在开始,您将能够使用json访问simplejson包。