Using Pytorch in AWS lambda Error- "Unable to import module 'lambda_function': No module named 'torch._C'",

Viewed 503

I am trying to use pytorch in AWS lambda but I keep getting this error. I am using arn:aws:lambda:us-east-1:934676248949:layer:pytorchv1-py36:2 as the layer for pytorch and this is my handler function

import unzip_requirements
import torch



def lambda_handler(event, context):
    
    return "Hello"

As in the function I am just only importing torch and it is giving me this error.

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'torch._C'",
  "errorType": "Runtime.ImportModuleError"
}
1 Answers

Make sure your AWS Lambda runtime and your container (when using a layer) or your EC2 instance (when installing on EFS from EC2) use the same Python version.
PyTorch C extensions are compiled for a specific Python version and discrepancies will result in the error you got.

Related