I'm trying to import pysftp in my AWS lambda function, but I'm getting this error: "Unable to import module 'app': cannot import name '_bcrypt' from partially initialized module 'bcrypt' (most likely due to a circular import)"

The only code I have written in my function is the basic default code plus my pysftp import:

import json
import pysftp
def lambda_handler(event, context):
    return {
        "statusCode": 200,
        "body": json.dumps({
            "message": "hello world",
            # "location": ip.text.replace("\n", "")
        }),
    }

bycrpt is a package that automatically gets installed when pysftp is installed, so when I try to import pysftp, it is connected to bycrpt. The way I connected all my libraries/packages to my lambda was by zipping together all the site-packages in my virtual environment into one folder and uploading that into a lambda layer. I then connected that layer to my function. It seems that the layer connection is working, because before I tried that method, I was getting the error "Unable to import module 'app': No module named 'pysftp'". So at least I'm not getting that error anymore.

I also tried zipping all my packages and my lambda function together in one folder and then uploading that zipped folder directly into my lambda function (as shown in this video: https://www.youtube.com/watch?v=yyBSeGkuPqk), but I'm getting the same error. Using the layer method is much simpler so I'd rather stick with that.

When I tried researching my specific error, all I could really find were people that had listed their imports in incorrect orders, or were importing packages multiple times. But I'm not doing either of those. I'm just trying to get the simple default code to work.

Any help is much appreciated! Thank you!

2

There are 2 answers

1
kaheicanaan On

It seems that bcrypt requires C or C++ libraries. The issue could be related to this. I got similar error message when I import pyzmq in Lambda function and it can be resolved by summiting a pyzmq package that is built in Linux environment.

And of course you can follow the instructions from AWS docs.

0
Harvastum On

I had a similar issue with another library, it turned out that the dependencies I installed locally were not compatible with AWS lambda environment. Specifying the target platform, as per AWS documentation, helped:

pip install \
    --platform manylinux2014_x86_64 \
    --target=my-lambda-function \
    --implementation cp \
    --python 3.9 \
    --only-binary=:all: --upgrade \
    -r requirements.txt

Have a look at pip docs if you want to know what each argument does.