'Cannot import name 'cygrpc' from 'grpc._cython' - Google Ads API

I want to deploy working python project in pycharm to aws lambda. The project is using google-ads library to get some report data from google ads.

I tried deploying lambda by importing complete project as a zip file by zipping all the folders/files inside the project and not the project folder itself. But i got the following error:

{
"errorMessage": "Unable to import module 'main': cannot import name 'cygrpc' from 'grpc._cython' (/var/task/grpc/_cython/__init__.py)",
"errorType": "Runtime.ImportModuleError",
"stackTrace": []
}

Assuming that google-ads library is working and that something is wrong with grpc(btw google-ads includes grpcio and stuff on its own), i tried to create a layer for grpcio, cython, cygrpc but the error remains same.

I create projects/layers in aws lambda and they work. I dont know what i am doing wrong here.

Any help would be really appreciated!

versions: google-ads-14.1.0, python-3.9, grpcio-1.43.0



Solution 1:[1]

Answering my own question after a lot of workaround. I have made it generic so anyone can use it.

I believe you can fix any type of ImportModuleError as long as your deployment package's file structure, your code and architecture is ok, only then you can deploy and run your code successfully. To fix your structure and architecture, follow steps below:

1- Install "ubuntu 18.04 LTS" from microsoft store (Windows 10).

2- Open CMD and run following commands:

  • ubuntu1804
  • Enter password or create user if asked.
  • cd /mnt/c You can choose any of your drive. I chose C.
  • mkdir my-lambda-folder Create project folder.
  • cd my-lambda-folder Enter into project folder.
  • touch lambda_function.py Create file called lambda_function.py
  • Now copy and paste your code into file you just created i.e lambda_function.py
  • pip install --target ./package your-module-name
  • For Example: pip install --target ./package google-ads will install google-ads module inside folder 'package'. The folder 'package' will be created automatically if not found.
  • cd package
  • zip -r ../my-deployment-package.zip . This will create deployment package with the installed library at the root of your project folder i.e my-lambda-folder.
  • cd .. go back to the root of your project folder.
  • zip -g my-deployment-package.zip lambda_function.py Add your lambda function to the deployment package you just created i.e my-deployment-package.zip.
  • (Optional) In my case i was using google-ads and to run my code i needed google-ads.yaml file too in my deployment package. So i ran additional command zip -g my-deployment-package.zip google-ads-yaml (i already pasted this file in my project folder).

3- Upload my-deployment-package.zip to your lambda function in AWS console and you are good to go.

Solution 2:[2]

For me it worked by just by diwnloading the packages with pip on ubuntu on docker then packing and uploading them on aws

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Abdullah Hafeez
Solution 2 Amir