简体   繁体   English

NameError: 名称 'json' 未定义

[英]NameError: name 'json' is not defined

Trying to import Json, my command is pip install json .尝试导入 Json,我的命令是pip install json I'm working on windows 8.1我正在使用 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 numpy and it worked just fine .我尝试导入numpy并且效果很好。 I also did check Pip "Could not find a that satisfies the requirement" and Could not find a version that satisfies the requirement <package>我还检查了Pip“找不到满足要求的版本找不到满足要求的版本 <package>

Edit : Referred also to this link Python 3.5.1 : NameError: name 'json' is not defined and getting an error that sudo is not recognized编辑:也参考此链接Python 3.5.1 : NameError: name 'json' is not defined并收到无法识别 sudo 的错误

If it's not defined in your code, you need to import it.如果它没有在你的代码中定义,你需要导入它。 This is exactly the same as any name in Python;这与 Python 中的任何名称完全相同; you can't use something until you have defined it.在你定义它之前你不能使用它。

import json

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 有时会起作用。

I have also encountered a similar issue, pip failing to install json and math modules (using python 3.x).我也遇到过类似的问题,pip 无法安装 json 和 math 模块(使用 python 3.x)。 Finally, I discovered that some modules you simply do not have to install - they are already BUILT-IN.最后,我发现有些模块你根本不需要安装——它们已经内置了。 :) Of course, you still have to add "import json" at the top of your .py file. :) 当然,您仍然需要在 .py 文件的顶部添加“import json”。 Hope this helps somebody.希望这可以帮助某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM