Python relative imports in AWS Lambda fail with attempted relative import with no known parent package
In AWS Lambda if I attempt an explicit relative import like this
.
├── lambda_file.py
└── example.py
# lambda_file.py
from .example import lambda_handler
# example.py
def lambda_handler(event, context):
return True
And I configure AWS Lambda's handler to lambda_file.lambda_handler
I get the following errors
- Python 2.7 :
Attempted relative import in non-package
- Python 3.7 :
attempted relative import with no known parent package
PEP008 says :
Implicit relative imports should never be used and have been removed in Python 3.
If I change lambda_file.py
to contain the following, it works, but no longer uses explicit relative imports
# lambda_file.py
from example import lambda_handler
The solution is to ensure that the "Handler" value that you configure in AWS Lambda contain at least 2 .
periods. To achieve this you need to put your code within a directory in your AWS Lambda code zip file and make that directory a module by adding an empty __init__.py
file. The resulting structure looks like this
.
├── app
│ ├── __init__.py
│ ├── lambda_file.py
│ └── example.py
And you now change the "Handler" value from lambda_file.lambda_handler
to app.lambda_file.lambda_handler
- AWS Lambda loads your handler as a module import not as a top-level script
- This solution came after reading this explanation of explicit relative imports by Brendan Barnwell
@AustinGilkison
Thanks for your input, it works perfectly fine for files that exist inside container.
Let say If I want to call the handler file located in EFS file system and its mounted to Lambda function with the mount path "/mnt/xxxfolder" .
I have copied the 'init.py' in all the folder structure defined till handler function, but Its still failing with
[ERROR] TypeError: the 'package' argument is required to perform a relative import for '.mnt.xxxfolder.yyfolder.lambda_handler'
Traceback (most recent call last):
File "/var/lang/lib/python3.9/importlib/init.py", line 122, in import_module
raise TypeError(msg.format(name))
Appreciate your comments. Thanks.