6

Problem

I'm trying to split my python code for a lambda function across multiple files however any attempt to import the other relative modules throws an error for the top level module.

{
  "errorMessage": "Unable to import module 'download_ga_data'"
}

What am I doing wrong? This feels like it should be super basic.

File structure layout (shown from root)

- download_ga_data.py
   - [analytics]
        - google.py (contains a single class)
        - __init__.py
   - [helpers]
        - main.py (contains a single class)
        - __init__.py
   - {other libraries from site-packages}

Contents of download_ga_data.py

# import unicodecsv as csv
import os

# import path
from . import definitions
from analytics.google import GoogleAnalytics
from helpers.main import GoogleCloudStorageBucket

def lambda_handler(event, context):
    print("test")

This as it stands will throw the error. If I comment out the three imports after os, then it will function correctly.

How should I correctly import these two modules, I feel like I'm missing something super basic.

Environment notes

This is all built on a the following lambda mimicking docker and uploaded straight into S3. All the files are 777 to bypass any permissions errors.

2
  • 1
    should it be from helpers.main import GoogleCloudStorageBucket ? Commented Aug 1, 2018 at 2:59
  • @maxymoo oop that's a mis-type in the original question. Fixed. Commented Aug 1, 2018 at 19:39

1 Answer 1

2

Ok finally solved this. The main tip I received to help with this was to wrap my load functions in a try except block:

try:
    import definitions
    from analytics.google import GoogleAnalytics
    from cloud_helpers.main import GoogleCloudStorageBucket
except Exception as e:
    error = str(e)
    stacktrace = json.dumps(traceback.format_exc())
    message = "Exception: " + error + "  Stacktrace: " + stacktrace
    err = {"message": message}
    return respond(err)

def respond(err, res=None):
    return {
        "statusCode": "400" if err else "200",
        "body": err["message"] if err else json.dumps(res),
        "headers": {"Content-Type": "application/json"},
    }

This then revealed the following error (which I irritatingly only have a screenshot of):

enter image description here

Which was then solved by switching to import definitions

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.