模块jmespath没有属性search

问题描述 投票:0回答:2

我遇到了那个错误:

导入jmespath AttributeError: 模块 jmespath 没有属性搜索

我用的是这个模块的1.0.1版本

我想运行以下代码:

import jmespath
person={
    "person":[
        {"id":1,"name":"ali","age":42,"children":[
            {'name':'sara','age':7},
            {'name':'sima','age':15},
            {'name':'sina','age':2}
        ]},
        {"id":2,"name":"reza","age":65,"children":[]}
    ]
}

print(jmespath.search('person[*].children[?age>`10`].name', person))
python attributeerror jmespath
2个回答
0
投票

您使用的是哪个版本的 Python?如果是旧版本,你可以尝试用更高版本的 Python 运行代码。


0
投票

在给定文件层次结构的情况下,这是我能够重现它的一种方式:

.
├── demo.py
└── jmespath.py

文件jmespath.pyempty和包含您的代码的demo.py文件,我在执行它时确实遇到了这个错误:

$ python3 demo.py 
Traceback (most recent call last):
  File "demo.py", line 13, in <module>
    print(jmespath.search('person[*].children[?age>`10`].name', person))
          ^^^^^^^^^^^^^^^
AttributeError: module 'jmespath' has no attribute 'search'

因此,一种可能的修复方法是确保您没有名为

jmespath.py
的文件或名为
jmespath
的模块,它们会与库发生冲突。

© www.soinside.com 2019 - 2024. All rights reserved.