Python JSON module has no attribute 'dumps'

前端 未结 10 1400
攒了一身酷
攒了一身酷 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:37

    Do you have a file named json or simplejson in your path that isn't one of those two libraries? If you do, then python will load that file instead of the real library.

    0 讨论(0)
  • 2021-01-01 09:38

    Even I was facing similar error while running json.dump(). In my case I was getting an error string :

    AttributeError: 'file' object has no attribute 'dump'

    How I fixed it -

    I was using variable name as "json" for File descriptor in the same script, which is why I was getting this error. So I simply renamed that variable name and issue resolved.

    0 讨论(0)
  • 2021-01-01 09:44

    The mistake i did I name the file name as json.py. I got the error:

    AttributeError: partially initialized module 'json' has no attribute 'dumps' (most likely due to a circular import).

    I renamed the file name to json1.py instead of creating a new file.

    Hope it helps

    0 讨论(0)
  • 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'.

    0 讨论(0)
提交回复
热议问题