Some AWS lambda functions stopped working with "No module named setuptools._distutils" error

Viewed 543

I have an application with many serverless functions deployed to AWS lambda. These functions use Python 3.7 runtime environment. But yesterday after deploying some minor changes, few of these functions stopped working with the error.

[ERROR] Runtime.ImportModuleError: Unable to import module 'functions.graphql.lambda.user_balance': No module named 'setuptools._distutils'

The weird thing is that the functions which are throwing this error were not changed and other functions are working without any issues. No python module was added/removed.

Just to check if the code change has anything to do with this error, I tried deploying a previous version. But the error persists.

I used the serverless framework for deployment.

2 Answers

It looks like this is an issue that started happening for all Python users as of yesterday as the set up tools got updated, but PIP did not.

According to this GitHub issue there is a temporary workaround until this is actually fixed.

Setting environment variable SETUPTOOLS_USE_DISTUTILS=stdlib is a workaround, e.g.:

export SETUPTOOLS_USE_DISTUTILS=stdlib
pip3 install ....

My assumption would be that you could add this as an environment variable for the Lambda possibly through the serverless config?

This is the bug in setuptools https://github.com/pypa/setuptools/issues/2353. Follow the below temporary workaround.

Linux

export SETUPTOOLS_USE_DISTUTILS=stdlib

Windows

set SETUPTOOLS_USE_DISTUTILS=stdlib

After that, execute the pip command.

pip install XXXXX
Related