0

Using Python 3.7 in Lambda, how to import a function run() from sample.py in lambda_function.py

Here's my code: File1 - lambda_function.py:

import sample.py

def lambda_handler(event, context):
    sample.run()

    return {
        'statusCode': 200
    }

File 2 - sample.py:

def run()
    Print('success')

    return {
         'statusCode': 200
       }

Error: { "errorMessage": "Unable to import module 'sample': No module named 'sample.py'; 'sample' is not a package", "errorType": "Runtime.ImportModuleError" }

Project structure in lambda:

enter image description here

1 Answer 1

2

You should be able to import sample (without the py). If the error still persists, then you can add the current path of the other file (literally current path) to the PATH variable.

import sys sys.path.append("PATH_TO_SAMPLE")

Note, might be helpful to use os.getcwd() to see what directory your code is running in. Then you can get a correct relative path.

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

1 Comment

Removing .py made it work.

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.