How to fix import error "No module named '_cffi_backend' " when import pysftp from aws-lambda
Asked Answered
H

4

7

I'm working on a simple script to connect my sftp server from aws-lambda and I'm getting

Unable to import module 'lambda_function': No module named '_cffi_backend'

when I import pysftp from aws-lambda. I'm using python3.6 and only import pysftp nothing more

I already try to install cffi

python3 pip install cffi
Haruspicy answered 9/8, 2019 at 21:57 Comment(1)
Did you resolve it in the end?Idona
M
9

I've faced with the same issue on python 3.7 (cffi==1.11.2, cryptography==2.1.2, paramiko==2.3.1) and resolved it downgrading to python 3.6.

Found the solution in this issue topic.

Merits answered 8/10, 2020 at 10:41 Comment(3)
This fixed my issue. I was using Python 3.8 when my cffi was compiled for 3.6. You can tell which version of Python your cffi file has been compiled for in its name (for example _cffi_backend.cpython-36m-x86_64-linux-gnu.so). You can downgrade to Python 3.6 on the lambda console (for example https://<region>.console.aws.amazon.com/lambda/home?region=<region>#/functions).Mucronate
Thanks for this comment, I spent half day, trying different versions of lamdalayer.Crain
My problem was _cffi_backend for python 3.9. I had to back my lambdas down to 3.8 and use AWS method for making a layer. aws.amazon.com/premiumsupport/knowledge-center/…Kopaz
H
5

This worked for me, and it was an easy fix. Maybe it could help someone else.

After trying -
pip3 -vvv install --upgrade --force-reinstall cffi

pip install cffi

I got the following module _cffi_backend.cp39-win_amd64.pyd from a working environment and placed it inside the site-packages of the missing location with the issue.

Huertas answered 16/12, 2022 at 13:1 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Welch
A
1

You need to include the third party modules in the Lambda package. Go to the directory where pip keeps the data for your modules, find the modules you're using, copy their directories and include in the Lambda zip file. Then deploy again on Lambda and run it. Should work.

Autobahn answered 9/8, 2019 at 22:37 Comment(0)
D
-2

You need to upload the dependencies to the lambda function. All you need to do is create a folder, lets call it 'test' and put your python code in it. Then, install the required python packages into the same folder. You can do it using the following command:

pip install --target <path directory> <package name>

This installs the required packages into the specified directory. In your case, the command will be

pip install --target C:\test requests

Diagraph answered 11/8, 2019 at 14:36 Comment(1)
That is not answering OP's question AND you are assuming a Windows env.Ambiversion

© 2022 - 2024 — McMap. All rights reserved.