在Ubuntu上安装"pip install json"失败

49

无法安装json模块。据我所知,我不应该使用sudo。怎么回事?

 pip install json
The directory '/home/snow/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/snow/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting json
  Downloading json-99.0.tar.gz
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/tmp/pip-build-YkvqVh/json/setup.py", line 2, in <module>
        raise RuntimeError("Package 'json' must not be downloaded from pypi")
    RuntimeError: Package 'json' must not be downloaded from pypi

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-YkvqVh/json/

2
raise RuntimeError("Package 'json' must not be downloaded from pypi")这段代码有什么不清楚的地方吗? - Martijn Pieters
10
如果出现这种情况,应该说“包是不必要的”。如果我被告知需要安装一个软件包,而安装程序说我无法从 X 下载它,合理的假设是我需要从其他地方下载它。但在哪里? - Keith Tyler
2个回答

111

json 是一个内置模块,不需要使用 pip 安装。


3
这是一个内置模块,无法使用 pip 安装。如果你使用的是 Ubuntu,就像上面的用户一样,你可能已经安装了 Python2,但是你没有安装 json 模块。请查看我的回答。 - MichielB

14

虽然 json 是一个内置模块,但我发现在安装了 python-minimal 的 Ubuntu 系统上,你确实有 python,但是你无法执行 import json。那么我明白你会尝试使用 pip 安装该模块!

如果你安装了 python-minimal,那么你得到的 python 版本将比通常自己编译 python 得到的版本少一些模块,其中一个缺失的模块是 json 模块。解决方法是安装一个名为 libpython2.7-stdlib 的附加软件包,以安装所有“默认” Python 库。

sudo apt install libpython2.7-stdlib

然后你可以在Python中执行import json,它会起作用!


非常感谢您的见解,@MichielB。 - Eugenio

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接