【问题标题】:No module named json in python3python3中没有名为json的模块
【发布时间】:2021-05-28 07:40:05
【问题描述】:

我想在我的 python 脚本中使用 json 解析器。 当我导入 json 模块时,会出现 ImportError:

root@msc-nr-imx6x:~# python3 
Python 3.5.2 (default, Aug  9 2017, 22:59:34) 
[GCC 6.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import json
Traceback (most recent call last):
   File "<stdin>", line 1, in <module>
ImportError: No module named 'json'
>>> 

我认为在 python3 中,json 是内置的。 Python 正在运行 - arm imx6、yocto 2.2、python 3.5.2

那么我该如何安装json模块呢?

谢谢

编辑 - 添加 test.py 脚本的输出:

try:
    import json
except ImportError:
    import simplejson as json

输出:

root@msc-nr-imx6x:~# python3 test.py 
Traceback (most recent call last):
  File "test.py", line 2, in <module>
    import json
ImportError: No module named 'json'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 4, in <module>
    import simplejson as json
ImportError: No module named 'simplejson'

【问题讨论】:

    标签: json python-3.x embed yocto


    【解决方案1】:

    Yocto 创建嵌入式系统:这就是为什么 python 不会自动附带它通常使用的所有模块的原因。创建您正在使用的图像的人可以选择包含所有标准模块,但没有这样做。

    如果您正在构建自己的映像,您可以在映像安装中包含更多 python 包,python3-json 满足您的需要,或者类似python3-modules 来获取所有常见的包:请参阅 meta/recipes-devtools/ python/python3/python3-manifest.json 了解更多关于拆分的详细信息。

    编辑:实际上,您使用的是旧的 Yocto 版本,因此可能想查看 meta/recipes-devtools/python/python-3.5-manifest.inc 了解详细信息

    【讨论】:

    • 我运行 - bitbake python-json,然后我在部署中复制文件(目录 lib-dynload/ 和 json),现在它正在工作。谢谢
    【解决方案2】:

    在你的情况下,你可以试试这个代码 sn-p:

    try:
        import json
    except ImportError:
        import simplejson as json
    

    您可以访问python json module import error

    【讨论】:

    • 感谢您的回复,我添加了脚本的输出。我没有 simplejson(就像在链接中一样 - 他们使用 python2.6)
    • json 这个默认的python包。如果您没有 json,那么您可以升级您的 python 版本,或者如果想要使用相同的安装 python 然后安装 json (pip install jsons)[pypi.org/project/jsons/]
    • 它是嵌入式linux,没有pip(而且我无法安装pip,因为yocto...)
    • 最后一个选项是升级python
    猜你喜欢
    • 2018-08-16
    • 2020-10-23
    • 2021-09-27
    • 1970-01-01
    • 2021-11-09
    • 2011-05-18
    • 2018-01-20
    • 2019-01-26
    • 1970-01-01
    相关资源
    最近更新 更多