Python NameError: 名称 'json' 未定义

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47412518/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 18:12:11  来源:igfitidea点击:

NameError: name 'json' is not defined

pythonjsonpip

提问by Jimmy

Trying to import Json, my command is pip install json. I'm working on windows 8.1

尝试导入 Json,我的命令是pip install json. 我正在使用 Windows 8.1

The error i'm getting in command prompt is

我在命令提示符中遇到的错误是

Could not find a version that satisfies the requirements json <from versions:>

No matching distribution found for json.

No matching distribution found for json.

and the error i'm getting on pycharm is

我在 pycharm 上遇到的错误是

NameError: name 'json' is not defined

I tried importing numpyand it worked just fine . I also did check Pip "Could not find a that satisfies the requirement"and Could not find a version that satisfies the requirement <package>

我尝试导入numpy,效果很好。我也确实检查了Pip“找不到满足要求的版本并且找不到满足要求的版本 <package>

Edit : Referred also to this link Python 3.5.1 : NameError: name 'json' is not definedand getting an error that sudo is not recognized

编辑:也参考此链接Python 3.5.1 : NameError: name 'json' is not defined并收到无法识别 sudo 的错误

回答by Daniel Roseman

If it's not defined in your code, you need to import it. This is exactly the same as anyname in Python; you can't use something until you have defined it.

如果它没有在你的代码中定义,你需要导入它。这与Python 中的任何名称完全相同;在你定义它之前你不能使用它。

import json

回答by SBN AB

Use the command as simplejson instead of json like below,

使用命令作为 simplejson 而不是像下面这样的 json,

pip install simplejson

pip 安装 simplejson

If the above command also throwing an error Use the following command

如果上面的命令也抛出错误使用下面的命令

easy_install simplejson

easy_install simplejson

easy_install works sometimes if pip cannot serve the request.

如果 pip 无法满足请求,easy_install 有时会起作用。