【问题标题】:python json module import errorpython json模块导入错误
【发布时间】:2016-06-26 20:04:29
【问题描述】:

我在使用 python json 模块时收到以下错误

# python
Python 2.4.3 (#1, Jan 11 2013, 02:09:42) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
ImportError: No module named json
>>> 

有人可以帮我吗?

【问题讨论】:

  • 除非你需要维护旧的东西,否则你应该升级你的 python 2.4 已经很旧了?

标签: python json


【解决方案1】:

使用 simplejson 就可以了

# python
Python 2.4.3 (#1, Jan 11 2013, 02:09:42) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import simplejson
>>> 

【讨论】:

    【解决方案2】:

    不用担心您的 Python 版本和(简单)可移植性:

    try:
        import json
    except ImportError:
        import simplejson as json
    

    【讨论】:

    • 你能解释一下上面的语句是做什么的,因为我是 python 新手
    【解决方案3】:

    您使用的是 Python 2.4.3,json 似乎是 python 2.6 中的新功能,请更新 python。

    【讨论】:

      【解决方案4】:

      正如 idjaw 上面评论的那样。

      你需要使用:

      import json
      

      (不是杰森)


      另外,您需要使用 Python 2.6 或更高版本。

      【讨论】:

      • import json 不会给你这个错误,因为它是一个有效的模块。
      • @ode2k 如果他使用的是旧蟒蛇就会这样
      【解决方案5】:

      请使用稳定的 python 2.7.x 来解决您的问题,然后尝试使用 json

      >>> import json
      

      您也可以使用 dir() 命令检查 python 编程中 json 模块附带的方法和类,使用 dir()

      >>> dir(json)
      ['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__doc__', '__file__', '__name__', '__package__', '__path__', '__version__', '_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
      

      【讨论】:

        猜你喜欢
        • 2012-07-14
        • 2023-03-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-12-20
        相关资源
        最近更新 更多