首页 > 解决方案 > ImportError:无法从“json”(未知位置)导入名称“loads”

问题描述

Previos 标题是: AttributeError: module 'json' has no attribute 'loads' 我更改了它,因为它看起来与类似,但在我提供的链接中,问题似乎是该人有一个名为的文件,该文件json.py欺骗了导入,以为导入的是本地文件而不是来自的 json标准库。我的问题是我没有任何名为的本地文件json.py

我想知道它是否必须做与 PATH 或我的项目结构相关的任何事情。任何建议都可能有所帮助。

错误回溯:

  File "D:\Me\IdeaProjects\src\app\repositories\user_repository.py", line 14, in get_user
    user = json.loads(file.read())

我在 Windows 10 和 intelliJ ide 中运行代码。

Python版本:3.7.4

尝试了官方文档中的代码

import json
def as_complex(dct):
    if '__complex__' in dct:
        return complex(dct['real'], dct['imag'])
    return dct

json.loads('{"__complex__": true, "real": 1, "imag": 2}', object_hook=as_complex)

并且也得到了这个错误:

Traceback (most recent call last):
  File "<pyshell#7>", line 1, in <module>
    json.loads('{"__complex__": true, "real": 1, "imag": 2}',object_hook=as_complex)
AttributeError: module 'json' has no attribute 'loads'

当我尝试loads显式导入时,出现此错误:

ImportError: cannot import name 'loads' from 'json' (unknown location)

标签: pythonjson

解决方案


我在窗口 10 的管理员帐户中安装了 python,并且它以管理员权限安装,但是当我在另一个帐户中使用时,我无法使用这些包,但是在当前帐户中安装 python 确实解决了问题。


推荐阅读