Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Having trouble importing validate from jsonschema #48

Open
zanylane opened this issue Feb 21, 2018 · 2 comments
Open

Having trouble importing validate from jsonschema #48

zanylane opened this issue Feb 21, 2018 · 2 comments

Comments

@zanylane
Copy link

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
Copy link
Contributor

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
Copy link
Author

zanylane commented Feb 22, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants