'Serverless Python Local Module Not Found
I am looking to import my local python module file into my handler file in my serverless project, but despite this local file being located in the parent directory with my handler file it doesn't appear to recognize the module. I am relatively new to the python serverless setup and am wondering if there is something missing about how serverless file importation works.
Here are the files:
/ (parent directory)
data.py
gather_keys_oauth2.py
Error Message:
Proxy Handler could not detect JSON: File "/Users/user/.nvm/versions/node/v12.14.0/lib/node_modules/serverless/lib/plugins/aws/invokeLocal/invoke.py", line 72, in <module>
module = import_module(args.handler_path.replace('/', '.'))
File "/Users/user/miniconda3/lib/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "./data.py", line 4, in <module>
import gather_keys_oauth2 as Oauth2
ModuleNotFoundError: No module named 'gather_keys_oauth2'
data.py:
import json
from datetime import timedelta, datetime
import math
import gather_keys_oauth2 as Oauth2
import fitbit
def auth(event, context):
CLIENT_ID = '*client*'
CLIENT_SECRET = '*secret*'
server = Oauth2.OAuth2Server(CLIENT_ID, CLIENT_SECRET)
server.browser_authorize()
ACCESS_TOKEN = str(server.fitbit.client.session.token['access_token'])
REFRESH_TOKEN = str(server.fitbit.client.session.token['refresh_token'])
auth2_client = fitbit.Fitbit(CLIENT_ID, CLIENT_SECRET, oauth2=True, access_token=ACCESS_TOKEN, refresh_token=REFRESH_TOKEN)
print(auth2_client)
body = {
"message": "Auth",
"input": event
}
response = {
"statusCode": 200,
"body": body['message']
}
return response
Solution 1:[1]
Your lambda is not packaging the module you wish to import. You can use serverless-python-requirements plugin for this.
It can be installed locally or on a pipeline with
sls plugin install -n serverless-python-requirements
You can add this to you serverless.yml file and try deploy
# this part might not be needed depending on size of utils
custom:
pythonRequirements:
zip: true
# This plugin allows us import dependencies
plugins:
- serverless-python-requirements
Check out the guide on the plugin here
https://www.serverless.com/blog/serverless-python-packaging
https://www.npmjs.com/package/serverless-python-requirements (URL update) You can use
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Sufiyan Ansari |