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

rab*_*010 7 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")
Run Code Online (Sandbox Code Playgroud)

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

fer*_*rdy 16

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

$ sudo pip3 install simplejson
Run Code Online (Sandbox Code Playgroud)

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

import simplejson as json
Run Code Online (Sandbox Code Playgroud)

从现在开始,您将能够使用simplejson包json.