Python JSON module has no attribute 'dumps'

前端 未结 10 1855
攒了一身酷
攒了一身酷 2021-01-01 09:14

I am running Python 2.7 (x64 Linux) and trying to convert a dict to a JSON object.

>>> import sys
>>> sys.version_info
sys.ver         


        
10条回答
  •  盖世英雄少女心
    2021-01-01 09:45

    I have discussed the problem and solution below

    Problem: when you call the import function on python, python try to find the module inside your working directory, if it does not find inside your local directory, that's when it will look for standard libraries and other global repos. Hence, if you try to import json module, then it'll try to find it inside your directory and If you have a file named 'json.py', python will load this module and tries to find 'dump' function inside your json.py. if it does not find 'dump' function inside your file, it will throw an error 'module' object has no attribute 'dumps'.

    Resolution: rename your 'json.py' file into different name: like json_file.py or anything but 'json.py'.

提交回复
热议问题