Python 3.5.1:NameError:未定义名称“json" [英] Python 3.5.1 : NameError: name 'json' is not defined

查看:50
本文介绍了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.上面是我定义import json的区域.

The python compiler says line 54 is wrong. I already defined import json as simplejson. The above area where I defined import json is showed above.

推荐答案

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

You should at first install simplejson to your system to be able to import it:

$ sudo pip3 install simplejson

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

Then in your code you are now able to import it:

import simplejson as json

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

From now on you will be able to access simplejson package using json.

请注意,如果您仍在使用 python 2 - 尽管这不是 OP 的原始问题 - 您应该做一个

Please note, if you're still using python 2 - despite this is not the OPs original question - you should do a

$ sudo pip install simplejson

相反.

这篇关于Python 3.5.1:NameError:未定义名称“json"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆