alexa-samples / alexa-smarthome

Resources for Alexa Smart Home developers.
https://alexa.design/smarthome
Other
686 stars 336 forks source link

Having trouble importing validate from jsonschema #48

Open zanylane opened 6 years ago

zanylane commented 6 years ago

I'm working my way through the guide here: https://github.com/alexa/alexa-smarthome/wiki/Build-a-Working-Smart-Home-Skill-in-15-Minutes

I have everything set up, and when I ask Alexa to discover devices, all I get is an error in the logs:

Unable to import module 'lambda': cannot import name 'validate'

If I change the Python version to 2.7 then it can't even find the module jsonschema. Is there some other thing that I'm doing wrong? I can provide plenty of extra details, but really, I just went step by step through that guide, and it's just not working, although I have everything connected, and was able to enable the skill from the Alexa app on my phone. (Also can do it through the web interface from my PC, but either way seems the same result. Python can't find that method for some reason.)

Thanks!

briankel commented 6 years ago

Hi, some things to check:

  1. I'd suggest going back to Python 3.6.
  2. Is your "Handler" value in your lambda set to lambda.lambda_handler ?
  3. Is your lambda.py file in the root of your lambda directory and not nested into a subfolder?
zanylane commented 6 years ago

Thank you for your reply.

  1. I went back to 3.6 very soon after finding out that it didn't help. Actually it caused a different problem, so it didn't take me too long to switch back.

As for the other two points, it wasn't complaining that it couldn't find lambda. I commented out the lines wanting to validate the v3 json, and the code runs now. It just seemed like it could be a good idea to have that validation, just in case. (But since it's only code from Amazon's that I'm allowing inbound, I don't know how much of a risk it is.)

Thank you for your time! It really seems like their example code should run 'out of the box' but I don't know enough Python and how it treats imports to really understand it. I spent quite a while trying to puzzle it out, but I don't have enough time right now and was hoping that for an expert it would just be obvious.

Within the 'jsonschema' subfolder, there is a file 'validators.py' that contains the 'validate' method. There is a ' init .py' (no spaces, but the forum code was eating it and making the init render as bold) file in the jsonschema folder that has this: from jsonschema.validators import ( Draft3Validator, Draft4Validator, RefResolver, validate )

Here's the function, from the end of validators.py:

def validate(instance, schema, cls=None, *args, *kwargs): if cls is None: cls = validator_for(schema) cls.check_schema(schema) cls(schema, args, **kwargs).validate(instance)

So I really don't see why it would be failing. At least for now, I've given up on validation, until I can figure this out and fix it.