Python 3.5.1:NameError:名称'json'未定义

问题描述:

#!/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的上述区域。Python 3.5.1:NameError:名称'json'未定义

+0

显示请在你定义它。 –

+3

这就是为什么然后......你的意思是做'导入simplejson作为json'?不管怎么说,为什么不只是'import json'? –

+0

好吧,让我分享完整的代码 – rabeshi1010

你应该先安装simplejson到您的系统能够将其导入:

$ sudo pip3 install simplejson 

然后在你的代码,你现在可以将其导入:

import simplejson as json 

从现在起你将能够使用json访问simplejson软件包。