27

Trying to import Json, my command is pip install json. I'm working on 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.

and the error i'm getting on pycharm is

NameError: name 'json' is not defined

I tried importing numpy and 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>

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

Jimmy
  • 313
  • 1
  • 4
  • 9

3 Answers3

59

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

import json
Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
  • 1
    That's quite awkward, i've put the import json in a try-catch by mistake. – Jimmy Nov 21 '17 at 13:00
  • 1
    No need to install, just import json – Adithya Jul 08 '20 at 07:47
  • Folks, if you are working with airflow code, keep in mind that the code, as well python, can't have any "spaces" after declaring the variable (eg: response_filter=lambda response:json.loads(response.text),). So import the dependency and then delete any "abnormal" space between your variable(response_filter) and value(lambda response:json.loads(response.text),)). in other words, any spaces that you probably have on the edge of your "=". Just do a TRIM :) – Felipe Cabral Dec 05 '21 at 11:24
2

I have also encountered a similar issue, pip failing to install json and math modules (using 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. Hope this helps somebody.

Michael_86
  • 21
  • 1
0

Use the command as simplejson instead of json like below,

pip install simplejson

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

easy_install simplejson

easy_install works sometimes if pip cannot serve the request.

SBN AB
  • 161
  • 1
  • 1
  • 17