[Python] AttributeError: module ‘json’ has no attribute ‘loads’
[Python] AttributeError: module ‘json’ has no attribute ‘loads’
Introduction
In this article, we will demonstrate why the error AttributeError: module 'json' has no attribute 'loads'
in Python occurs and how to solve it. Let’s try to reproduce the problem.
Reproducing the problem
- Create a Python program named
json.py
. This is just a simple program to load json data from a string and output the data we need.
json.py
#!/usr/bin/env python3
import json
sample_json = '{ "name":"John", "age":30}'
data = json.loads(sample_json)
print(f'{data["name"]} is {data["age"]} years old...')
- Run the program
json.py
and BOOM! We have just reproduced the problem!
noob@learnfromnoobs:~$ ./json.py
Traceback (most recent call last):
File "json.py", line 1, in <module>
import json
File "/home/noob/json.py", line 4, in <module>
data = json.loads(sample_json)
AttributeError: module 'json' has no attribute 'loads'
Error in sys.excepthook:
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/apport_python_hook.py", line 63, in apport_excepthook
from apport.fileutils import likely_packaged, get_recent_crashes
File "/usr/lib/python3/dist-packages/apport/__init__.py", line 5, in <module>
from apport.report import Report
File "/usr/lib/python3/dist-packages/apport/report.py", line 30, in <module>
import apport.fileutils
File "/usr/lib/python3/dist-packages/apport/fileutils.py", line 23, in <module>
from apport.packaging_impl import impl as packaging
File "/usr/lib/python3/dist-packages/apport/packaging_impl.py", line 18, in <module>
import json
File "/home/noob/json.py", line 4, in <module>
data = json.loads(sample_json)
AttributeError: module 'json' has no attribute 'loads'
Original exception was:
Traceback (most recent call last):
File "json.py", line 1, in <module>
import json
File "/home/noob/json.py", line 4, in <module>
data = json.loads(sample_json)
AttributeError: module 'json' has no attribute 'loads'
How to fix the problem?
If you just want to fix the problem as soon as possible and don’t care about the reason behind, just rename your Python program to something other than json.py
.
For example,
noob@learnfromnoobs:~$ mv json.py json_test.py
noob@learnfromnoobs:~$ ./json_test.py
John is 30 years old...
If that didn’t solve your problem, maybe that’s because you have generated a json.pyc
as well next to your json.py
. Make sure to remove that also. Otherwise, json.pyc
will still be referenced and the problem will still occur.
So what really happened?
To understand what caused the issue, we first need to understand how import works.
When we import a module in Python, it tries to find the module specified and then load it. It looks into sys.path
and see if the module required can be found in one of the paths there. Let’s take a took at the sys.path
in Python interactive shell.
noob@learnfromnoobs:~$ python3
Python 3.6.8 (default, Oct 7 2019, 12:59:55)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.path
['', '/usr/lib/python36.zip', '/usr/lib/python3.6', '/usr/lib/python3.6/lib-dynload', '/usr/local/lib/python3.6/dist-packages', '/usr/lib/python3/dist-packages']
sys.path
contains a list of locations to find in when searching for a module. Note that the current working directory is denoted by an empty string (”).
According to the official document for sys.path
, the first location in sys.path
is the directory containing the script. As we were just testing in the interactive shell, there was no script directory. As a result, the first location in sys.path
became an empty string, indicating that it will look for the modules in the current working directory first.
In our case, we named our program json.py
and we are trying to import the module json
at the same time. As the script directory is the first path in our sys.path
, it first searches in the script directory for the json
module, which obviously, is the wrong module it is looking for. There is no attribute named ‘loads’ in our json.py
. That’s why the error AttributeError: module 'json' has no attribute 'loads'
shows up.
Conclusion
In this article, we covered why the error AttributeError: module 'json' has no attribute 'loads'
shows up. How did I find this out? I must admit that I was too lazy to name my program as json.py
for some quick test, but this is a great experience and I noticed that it might be great to share about this. Keep in mind that you should avoid using the module name as your script name, or you will face this kind of error again.
Note that we oversimplified the process of importing in Python in this article. If you want to learn more about the importing process, please refer to the official document about import.
I hope you enjoyed this article and learned something new.
Keep learning and have fun!