安装 simplejson 后没有名为 'json' 的模块

No module named 'json' after installing simplejson

我在 Ubuntu 14.04 中工作,我的机器上有多个版本的 Python(它们包括 python2.7 和 python3.4)。几天前,我在我的系统上安装了 simplejson。我不记得我是怎么做到的,但我猜它一定类似于 pip install simplejson。但是,现在当我尝试安装任何 python 软件包时开始出现一个奇怪的问题。例如,刚才我尝试使用 sudo pip3.4 install Tkinter 安装 Tkinter 并抛出以下错误:

Traceback (most recent call last):
  File "/usr/local/bin/pip3.4", line 9, in <module>
    load_entry_point('pip==1.5.4', 'console_scripts', 'pip3.4')()
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 351, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2363, in load_entry_point
    return ep.load()
  File "/usr/lib/python3/dist-packages/pkg_resources.py", line 2088, in load
    entry = __import__(self.module_name, globals(),globals(), ['__name__'])
  File "/usr/lib/python3/dist-packages/pip/__init__.py", line 61, in <module>
    from pip.vcs import git, mercurial, subversion, bazaar  # noqa
  File "/usr/lib/python3/dist-packages/pip/vcs/subversion.py", line 4, in <module>
    from pip.index import Link
  File "/usr/lib/python3/dist-packages/pip/index.py", line 15, in <module>
    from pip.wheel import Wheel, wheel_ext
  File "/usr/lib/python3/dist-packages/pip/wheel.py", line 25, in <module>
    from distlib.scripts import ScriptMaker
  File "/usr/share/python-wheels/distlib-0.1.8-py2.py3-none-any.whl/distlib/scripts.py", line 15, in <module>
  File "/usr/share/python-wheels/distlib-0.1.8-py2.py3-none-any.whl/distlib/resources.py", line 20, in <module>
  File "/usr/share/python-wheels/distlib-0.1.8-py2.py3-none-any.whl/distlib/util.py", line 11, in <module>
ImportError: No module named 'json'

如果错误告诉我在我拥有的文件之一中,有时我可以解决此问题:

import json

我只是将其转换为

import simplejson as json

我试过简单卸载json:

sudo pip uninstall simplejson

但它给了我同样的错误:json 未找到。

任何人都可以帮我解决这个问题,以便我能够愉快地安装 python 包吗?提前致谢。

我猜您是使用 pip install simplejsonPyPI 下载或使用 apt-get install python-simplejson 从 ubuntu 存储库下载来安装它。

如果您使用上述任何命令,您可能下载了 Python2 的库,并且它不适用于 Python3(pip3.4 将使用)。您可以尝试这些命令并帮助自己进行调试吗?

$ python -c "import simplejson"

$ python3.4 -c "import simplejson"

这会告诉您上次安装 simplejson 的 python 是哪个版本(我在 python2 中的猜测)。如果第二个命令出现 ImportError 错误,请尝试:

$ pip3.4 install simplejson

然后安装您的库。

注意:我没有明确的答案,但会提供一系列您可以尝试的步骤:

第一件事是看看你是否可以从通常的 python 解释器导入 json:

import json
print(json.__file__) #this would be important to know if it works

如果这确实有效(以及评论 json.__file__ 是什么),那么您会想尝试 use pip from the interpreter.

如果无法正常导入json:

这并不奇怪,我没想到 pip 会在非标准的地方寻找模块。你会想弄清楚 json 包 应该 在你的计算机上的什么位置,你可以通过从标准库中导入另一个模块并查看它的 __file__:

>>> import fractions
>>> fractions.__file__
'/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/fractions.py'

这显然对您有所不同,但我希望在与 fractions.py

相同的文件夹中有一个 json 文件夹

如果您无法导入 fractionsqueuedatetime

如果您无法从标准库中导入任何内容,您可能只想重新安装 python。

如果 json 文件夹存在并且包含 __init__.py

使用你的文件浏览器的重命名功能来确保没有奇怪的特殊字符,但除此之外我不确定,如果你可以导入 fractions.py 但不是来自同一文件夹的包这意味着您的 python 版本的导入机制存在严重问题。

如果 json 文件夹不包含标准库的其余部分

您的 python 发行版可能与我预期的结构不同,至少看看它不会有什么坏处。

您可以在各种 python 文件 using the find command 中搜索 json 文件夹,不太确定它是如何工作的,但只是另一种尝试。如果你确实找到了 __init__.pyencode.pydecode.pyscanner.pytool.py(至少我的版本中是这样),你您可能想弄清楚它是如何到达那里的,但也许只是将它移动到与标准库的其余部分相同的文件夹中。

如果您找不到 json 包或者您找到它但它已损坏

那么你需要更换它!别担心,这并不难,只需抓取一个 source release of python from the site 并从中提取 json 包,解压后 json 文件夹应该位于 [=31] =] 文件夹。只需 copy/move 将其添加到标准库的其余部分,你就可以开始了!


我希望这能帮助你调试正在发生的事情,这涵盖了我能想象到的所有场景,我很想知道哪一个解决了你的问题(或者你能弄清楚什么,所以我可以想出有更多选项)