Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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
like image 651
cphill Avatar asked Feb 18 '26 20:02

cphill


1 Answers

in data.py import the module gather_keys_oauth2 in the following way:

import .gather_keys_oauth2 as Oauth2

Please note the dot (.) before the module name.

like image 193
cs4r Avatar answered Feb 21 '26 10:02

cs4r