Serverless-python-requirements: Module initialization error

Created on 5 Sep 2017  ·  15Comments  ·  Source: UnitedIncome/serverless-python-requirements

I am using virtualenv and serverless-python-requirements for my Python deployment in AWS Lambda using serverless framework. But after upload when I am calling the function it is giving “module initialization error: [Errno 22] Invalid argument” error.

Here is my serverless.yml file https://gist.github.com/himadriganguly/f9571c09a3c01c79ebf920034f5e5e55

All 15 comments

Did you add import unzip_requirements to the top of your handler's .py file?

Yes, i did.

can you list the file contents of the zip serverless uploads? IE:

unzip .serverless/SERVICENAME.zip -d service
ls service

Here it is

  - node_modules
  - venv
  - handler.py
  - package.json
  - requirements.py
  - unzip_requirements.py
  - .requirements.zip

That looks fine. And it works fine (you'll need a try/except ImportError around the import unzip_requirements) with your venv when using sls invoke local -f FUNCTIONNAME?

After running, sls invoke -f FUNCTIONAME i am getting

No module named 'unzip_requirements'

Traceback (most recent call last):
  File "/usr/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/invoke.py", line 63, in <module>
    sys.stdout.write(json.dumps(result, indent=4))
NameError: name 'result' is not defined

As in the root there is no unzip_requirements.py file but it is within the .serverless/SERVICENAME.zip file.

Yup. Like I mentioned, you'll have to replace

import unzip_requirements

with

try:
    import unzip_requirements
except ImportError:
    pass

to deal with the fact that that module is only(and only available) needed when deployed. And of course make sure you've activated your virtualenv and pip installed your requirements.txt.

That second error about 'result' is not defined is due to a regression in serverless v1.21 (serverless/serverless#4186) so you'll have to downgrade serverless to v1.20.

Ok done the steps as described and now getting

result = handler(event, FakeLambdaContext())
  File "./handler.py", line 129, in main
    s3Object = event['Records'][0]['s3']
KeyError: 'Records'

This error is becase it is not getting the S3 event details as written in my python script. So can you tell me what I have to do now? Have to downgrade the serverless version to 1.20.2 and then do the deploy then it will work?

It seems like the module imports, which is the problem lambda was having (the new error is just because you didn't pass an event), so it should be working.. could you share your requirements.txt?

Here it is

boto3==1.4.7
botocore==1.7.3
docutils==0.14
jmespath==0.9.3
python-dateutil==2.6.1
s3transfer==0.1.11
six==1.10.0
unicodecsv==0.14.1
xlrd==1.1.0

Hrm.. none of those should pose a problem. Not sure what's wrong. sorry :cry:

@dschep Hi buddy. Error is fixed now, it is the serverless framework issue. They have fixed it on 1.21.1 release (https://github.com/serverless/serverless/releases/tag/v1.21.1).

Oh! That was a pretty nasty regression, it was also the root cause of #73.

Anyways thanks a lot for your time and support. Take care buddy.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

IanTayler picture IanTayler  ·  4Comments

nknotts picture nknotts  ·  5Comments

JulienMarliac picture JulienMarliac  ·  3Comments

amitm02 picture amitm02  ·  5Comments

sepulworld picture sepulworld  ·  5Comments