首页 > 解决方案 > MacOS Python 3.7.7 内部 json 模块已损坏;怎么修?

问题描述

我正在运行 MacOS Catalina 10.15.7,并打开了一个常规终端会话。是的,我在我的 shell 中运行tmux,但这从来都不是问题,直到昨天我的工作日中间突然出现。

我能想到的唯一与问题开始时间相关的事件是使用我的 IDE(IntelliJ IDEA,主要是一个带有一些用 Python 编写的操作工具的 Java 项目)并且当它声称这些模块不是时笨拙地选择了“安装”找到——正确的做法是简单地将 IDE 指向 Python 3.7 解释器,这是在机器上检查项目的一个非常标准的部分。

那里的许多答案都指向搜索错误的“json.py”模块,但是我们可以从以下内容中看到没有导入这样的错误模块。

如何“修复” 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

这是它的位置:

$ which python3
/usr/local/bin/python3

我相信这证实了我的记忆,它是通过以下方式安装的brew

$ brew uninstall python3
Error: Refusing to uninstall /usr/local/Cellar/python/3.7.7
because it is required by glib, graphviz and gts, which are currently installed.
You can override this and force removal with:
  brew uninstall --ignore-dependencies python3

但是,我尝试了brew reinstall python3无济于事。

标签: pythonjsonpython-3.xmacospython-3.7

解决方案


好吧,我想我稍后会处理“glib、graphviz 和 gts”,但对我来说,诀窍是完全删除 Python 3 的 brew 安装:

brew uninstall --ignore-dependencies python3

现在一切恢复正常:

$ 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}'

推荐阅读