AttributeError module 'json' has no attribute 'dumps'


Borislav Hadzhiev

Wed Apr 20 20222 min read


Photo by Peter Thomas

AttributeError module 'json' has no attribute 'dumps' #

The Python "AttributeError module 'json' has no attribute 'dumps'" occurs when we have a local file named and try to import from the json module. To solve the error, make sure to rename any local files named

attributeerror module json has no attribute dumps

Here is an example of how the error occurs in a file called
import json # ⛔️ AttributeError: module 'json' has no attribute 'dumps' json_str = json.dumps({'name': 'Alice', 'age': 30}) print(json_str) print(type(json_str))

The most likely cause of the error is having a local file named which shadows the json module from the standard library.

Make sure to rename your local file to something other than to solve the error.
import json json_str = json.dumps({'name': 'Alice', 'age': 30}) # 👇️ {"name": "Alice", "age": 30} print(json_str) # 👇️ <class 'str'> print(type(json_str))

You can access the __file__ property on the imported module to see whether it is shadowed by a local file.
import json print(json.__file__) # ⛔️ result if shadowed by local file # /home/borislav/Desktop/bobbyhadz_python/ # ✅ result if pulling in correct module # /usr/lib/python3.10/

A good way to start debugging is to print(dir(your_module)) and see what attributes the imported module has.

Here is what printing the attributes of the json module looks like when I have a file in the same directory.
import json # ⛔️ ['__builtins__', '__cached__', '__doc__', '__file__', # '__loader__', '__name__', '__package__', '__spec__'] print(dir(json))

If you pass a module object to the dir() function, it returns a list of names of the module's attributes.

If you try to access any attribute that is not in this list, you would get the "AttributeError: module has no attribute".

We can see that the imported json module doesn't have a dumps attribute, which makes it evident that we are shadowing the official json module with our local file.

If you try to import the json module in a file called, you would get a little different error message that means the same thing.
import json # ⛔️ AttributeError: partially initialized module 'json' has no attribute 'dumps' (most likely due to a circular import) json_str = json.dumps({'name': 'Alice', 'age': 30})

Renaming your file solves the error.

You can use the sys module to print all of the built-in module names.
import sys # 👇️ print all built-in module names print(sys.builtin_module_names)
Use the search field on my Home Page to filter through my more than 1,000 articles.