Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NameError: name 'json' is not defined

Tags:

python

json

pip

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

like image 670
Jimmy Avatar asked Nov 21 '17 11:11

Jimmy


People also ask

How do you fix NameError name is not defined?

The Python "NameError: name is not defined" occurs when we try to access a variable or function that is not defined or before it is defined. To solve the error, make sure you haven't misspelled the variable's name and access it after it has been declared.

Is not defined JSON?

The Python "NameError: name 'json' is not defined" occurs when we use the json module without importing it first. To solve the error, import the json module before using it - import json .

How do I load a JSON string in Python?

Use the json.loads() function. The json. loads() function accepts as input a valid string and converts it to a Python dictionary. This process is called deserialization – the act of converting a string to an object.

How do I read a JSON file in Python?

json.loads(): If you have a JSON string, you can parse it by using the json.loads() method.json.loads() does not take the file path, but the file contents as a string, using fileobject.read() with json.loads() we can return the content of the file. Example: This example shows reading from both string and JSON file.


2 Answers

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
like image 133
Daniel Roseman Avatar answered Oct 07 '22 22:10

Daniel Roseman


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.

like image 21
Michael_86 Avatar answered Oct 07 '22 21:10

Michael_86