I just wanted to check the operation of the library, but I was addicted to it.
For example, suppose you create a file called json.py
to try out the json module.
json.py
import json
obj = {
"id" : 1,
"name" : "hoge",
}
str = json.dumps(obj)
print(str)
The code doesn't seem to be a problem ...
This will result in an error when run.
$ python3 json.py
...
AttributeError: module 'json' has no attribute 'dumps'
It is said that the json module does not have dumps.
This is because the file name itself is json, so it was loaded in preference to the standard module.
Therefore, renaming it will solve the problem.
$ mv json.py json_test.py
$ python3 json_test.py
{"id": 1, "name": "hoge"}
By the way, even if you don't have the file name json, you can get the same result if json.py
exists in the same directory hierarchy.
You can also get the same result if you have a directory with the same name that contains __init__.py
.
$ mkdir ./json
$ touch ./json/__init__.py
$ python3 json_test.py
Traceback (most recent call last):
File "json_test.py", line 8, in <module>
str = json.dumps(obj)
AttributeError: module 'json' has no attribute 'dumps'
Recommended Posts