goldisfine
goldisfine

Reputation: 4850

Why can't I import a module?

I would like to be able to use the iter_errors function from the module jsonschema. I've imported the module, jsonschema, but cannot access 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.

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:

>>> 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

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)

Upvotes: 0

Views: 287

Answers (1)

Julian
Julian

Reputation: 3429

So you can update a module with pip as it says there by passing --upgrade (or -U).

pip install -U jsonschema

The latest release as of today is 2.0.0.

(iter_errors has been around for quite a while though).

Once you have a recent version, make sure that like the example shows you make a * validator * instance to call it on. It's a method of validators, not a function.

So if you do

from jsonschema import Draft3Validator

your example should produce what you want.

Upvotes: 1

Related Questions