Il modulo "json" di Python 3 è effettivamente danneggiato

Oct 02 2020

Sto facendo questa domanda qui e non su Stack Overflow perché sembra essere specifico per MacOS. Molte risposte indicano la ricerca di un modulo "json.py" difettoso, tuttavia possiamo vedere da quanto segue che non viene importato alcun modulo così difettoso.

Come "aggiusto" l'installazione di Python 3?

$ 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

Ecco dove si trova:

$ which python3
/usr/local/bin/python3

Ho provato brew reinstall python3inutilmente.

Risposte

benwiggy Oct 02 2020 at 21:14

Non posso riprodurre gli errori su due Mac qui, uno che esegue Python 3.7.4 su Mojave; e uno in esecuzione 3.8.3 su Big Sur. Ho installato python direttamente dai pacchetti su 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

Entrambi dicono "Clang 6.0", se pertinente.

È possibile che l'installazione sia danneggiata e sia necessario reinstallare python3. Se stai usando HomeBrew o simili, potresti doverli contattare per ricevere supporto.

CharneyKaye Oct 02 2020 at 21:24

Beh, suppongo che mi occuperò di "glib, graphviz e gts" in seguito, ma il trucco per me è stato rimuovere completamente l'installazione brew di Python 3:

brew uninstall --ignore-dependencies python3

E ora le cose sono tornate alla normalità:

$ python3
Python 3.7.3 (default, Apr 24 2020, 18:51:23) 
[Clang 11.0.3 (clang-1103.0.32.62)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
>>> d = {'a' : 100, 'b' : 200}
>>> json.dumps(d)
'{"a": 100, "b": 200}'