MacOS – Python 3 ‘json’ module is actually corrupted

macospython

I'm asking this question here and not on Stack Overflow because it appears to be MacOS specific. Many answers over there point to searching for a bad "json.py" module however we can see from the following that no such bad module is being imported.

How do I "fix" the Python 3 installation?

$ python3
Python 3.7.7 (default, Mar 10 2020, 15:43:33) 
[Clang 11.0.0 (clang-1100.0.33.17)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

>>> import json
>>> d = {'a' : 100, 'b' : 200}
>>> json.dumps(d)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: module 'json' has no attribute 'dumps'

>>> print(json.__file__)
None

Here's where it is located:

$ which python3
/usr/local/bin/python3

I've tried a brew reinstall python3 to no avail.

Best Answer

I cannot reproduce the errors on two Macs here, one running python 3.7.4 on Mojave; and one running 3.8.3 on Big Sur. I installed python directly from the packages on python.org.

>>> json.dumps(d)
'{"a": 100, "b": 200}'
>>> print(json.__file__)
/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/json/__init__.py

Both say "Clang 6.0", if that's relevant.

It may be that your installation is corrupted, and you need to reinstall python3. If you're using HomeBrew or similar, then you may need to contact them for support.