Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/348.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 为什么可以';我不能导入一个模块吗?_Python_Module - Fatal编程技术网

Python 为什么可以';我不能导入一个模块吗?

Python 为什么可以';我不能导入一个模块吗?,python,module,Python,Module,我希望能够使用模块中的iter\u errors功能。我已经导入了模块jsonschema,但无法访问iter\u错误 我怀疑这可能是因为模块需要更新,如果是这种情况,我该怎么做 我尝试重新安装它,python提示我使用“升级”命令,我不确定如何使用 Requirement already satisfied (use --upgrade to upgrade): jsonschema in /Library/Python/2.7/site-packages Cl 谢谢 再次评论: 下面是代

我希望能够使用模块中的
iter\u errors
功能。我已经导入了模块jsonschema,但无法访问iter\u错误

我怀疑这可能是因为模块需要更新,如果是这种情况,我该怎么做

我尝试重新安装它,python提示我使用“升级”命令,我不确定如何使用

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


再次评论:

下面是代码用法,它从validator类调用函数:

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
我的代码: 其中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)

因此,您可以通过传递
--upgrade
(或
-U
)来使用pip更新模块

截至今天的最新版本是2.0.0

iter_错误
已经存在了很长一段时间)

一旦有了最新版本,请确保如示例所示,创建一个*validator*实例来调用它。它是验证器的方法,而不是函数

所以如果你这样做了

from jsonschema import Draft3Validator

您的示例应该会产生您想要的结果。

您试图用来访问iter\U错误的实际代码是什么
import
只需将模块名称导入到名称空间中;您很可能需要
jsonschema.iter\u错误。这回答了您的问题吗?
from jsonschema import Draft3Validator