AWS Lambda: “ModuleNotFoundError No Module named _foo or foo” Solution

There are two reasons you might encounter this issue on AWS Lambda.

You’re attempting to import a library that isn’t available by default in the Lambda Runtime environment.

In which case you need to include the dependencies in the /python directory of your function package or included Lambda layer.

You’ve already done that but it’s still not working? Does it seem like the function is just denying reality?! Read on…

You’ve included libraries for the wrong version of Python

Depending on the module, python might insist that you provide the exact version that matches the Lambda runtime that you’re using in AWS. This means if you’re using Python 3.7 in AWS, but did a pip install in your build environment on Python 3.6, it might not work.

Easiest solution would be to ensure you have a build environment that can use pip3.7 install -target /targetdir -r requirements.txt to ensure you’ve got matching versions.

I’ve also observed what can only be described as OS-specific weirdness when trying to do this in MacOS with ostensibly the correct Python version. For safety I ended up performing my packaging builds using the Official Python 3.7 docker image, which worked perfectly.

Comments

About the Author

Avatar
Pete
Pete is the person that owns this website. This is his face. His opinions are his own except when they're not, at which point you're forced to guess and your perception of what is truly real is diminished that little bit more.