Tulasi
Tulasi

Reputation: 65

how to import a function from python file1 to file2 in Lambda

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

Upvotes: 0

Views: 444

Answers (1)

Emilie
Emilie

Reputation: 81

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.

Upvotes: 2

Related Questions