Python3.8 JSON module not working on Ubuntu 20.04 LTS

Viewed 620
File "/usr/lib/python3/dist-packages/jmespath/__init__.py", line 1, in <module>
    from jmespath import parser
  File "/usr/lib/python3/dist-packages/jmespath/parser.py", line 30, in <module>
    from jmespath import lexer
  File "/usr/lib/python3/dist-packages/jmespath/lexer.py", line 3, in <module>
    from json import loads
ImportError: cannot import name 'loads' from 'json' (unknown location)

Whenever I try to do "import json" in my python3.8 code, I get the following mentioned error. If I remove "import json" from the code, the python3.8 code works perfectly fine. Importing json is mandatory for me because there are multiple files where import json is used. I cannot change it everywhere. So the possible solution is to get the broken json fixed. It looks like my Ubuntu20.04 is broken.

Thanks in advance. Any help would be appreciable.

4 Answers

I was the same problem with my Python version on MacOS installed from homebrew. (3.9.6)

After investigation, I was an other json dependancies install in the global site-package (global python packages folder) that override the standard lib json.

So. I just deleted the json folder and a file in this global site-package folder and it work fine !

In my case :

rm -rf /usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/json
rm -f /usr/local/Cellar/python@3.9/3.9.6/Frameworks/Python.framework/Versions/3.9/lib/python3.9/site-packages/json_extensions-0.2.1-py3.9-nspkg.pth

From the discussion in chat

@juanpa.arrivillaga Problem is solved. Python was installed in 2 places. One in .local and other in root account. As soon as I removed the ./local/lib/python3.8/json , it automatically started pointing to /usr/lib/python3.8/json and the code worked. Thank you all for your support

My solution was similar to Albans. For me it all broke when IntelliJ tried to install boto3 and installed some packages to /usr/local/lib/python3.9/site-packages . I just removed the json folder and the json_extensions-0.2.1-py3.9-nspkg.pth file.

I was able to determine where the folder was from IntelliJ was because you can see the Extended Libraries > Python 3 . And although I had a Library root similar to Albans which was /usr/local/Cellar/python@3.9/3.9.7/Frameworks/Python.framework/Versions/3.9/lib/python3.9, there was no site packages in that folder. You can see the site-package folder right in that same location in IntelliJ and once you delete that json folder and pth file, it starts working again.

Edit: I looked into this further, and it installs json/schema into that folder. Not sure why it would install json.schema in the same json folder as a built in library.

json.schema folder: /usr/local/lib/python3.9/site-packages/json/schema/

Maybe you should try to only import json and use it's function instead, like this :

import json
json.loads(json_data)
Related