简体   繁体   English

为什么不能导入模块?

[英]Why can't I import a module?

I would like to be able to use the iter_errors function from the module jsonschema . 我希望能够使用模块jsonschema中iter_errors函数。 I've imported the module, jsonschema, but cannot access iter_errors. 我已经导入了jsonschema模块,但是无法访问iter_errors。

I suspect this might be because the module needs to be updated, and if this is the case, how do I do this? 我怀疑这可能是因为需要更新模块,如果是这种情况,我该怎么做?

I tried reinstalling it, and python prompted me to use the command 'upgrade', which I'm unsure how to use. 我尝试重新安装它,而python提示我使用命令“ upgrade”,但我不确定该如何使用。

Requirement already satisfied (use --upgrade to upgrade): jsonschema in /Library/Python/2.7/site-packages
Cl

Thanks! 谢谢!


RE COMMENT: 回复评论:

I'm following the code usage here , which calls the function from the validator class: 我在这里遵循代码用法,它从验证器类调用函数:

EX CODE: EX代码:

>>> schema = {
...     "type" : "array",
...     "items" : {"enum" : [1, 2, 3]},
...     "maxItems" : 2,
... }
>>> v = Draft3Validator(schema)
>>> for error in sorted(v.iter_errors([2, 3, 4]), key=str):
...     print(error.message)
4 is not one of [1, 2, 3]
[2, 3, 4] is too long

MY CODE: where x is sample JSON 我的代码:其中x是示例JSON

with open('gc_schema_test.json', 'r') as handle:
     schema = json.load(handle)

v = Draft3Validator(schema)
for error in sorted(v.iter_errors(x), key=str):
    print(error.message)

So you can update a module with pip as it says there by passing --upgrade (or -U ). 因此,您可以通过传递--upgrade (或-U )来使用pip更新模块,如其上所述。

pip install -U jsonschema

The latest release as of today is 2.0.0. 截至今天的最新版本是2.0.0。

( iter_errors has been around for quite a while though). iter_errors已经存在了一段时间了)。

Once you have a recent version, make sure that like the example shows you make a * validator * instance to call it on. 一旦有了最新版本,请确保像示例一样显示您创建一个* validator *实例以对其进行调用。 It's a method of validators, not a function. 这是验证程序的方法,而不是函数。

So if you do 所以如果你这样做

from jsonschema import Draft3Validator

your example should produce what you want. 您的示例应该产生您想要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM