简体   繁体   English

适用于 Python 和 AWS Lambda 的 Google Cloud Platform API 不兼容:无法导入名称“cygrpc”

[英]Google Cloud Platform API for Python and AWS Lambda Incompatibility: Cannot import name 'cygrpc'

I am trying to use Google Cloud Platform (specifically, the Vision API) for Python with AWS Lambda.我正在尝试将适用于 Python 的 Google Cloud Platform(特别是 Vision API)与 AWS Lambda 结合使用。 Thus, I have to create a deployment package for my dependencies.因此,我必须为我的依赖项创建一个部署包。 However, when I try to create this deployment package, I get several compilation errors, regardless of the version of Python (3.6 or 2.7).但是,当我尝试创建此部署包时,无论 Python 的版本是什么(3.6 或 2.7),都会出现一些编译错误。 Considering the version 3.6, I get the issue "Cannot import name 'cygrpc'".考虑到 3.6 版,我收到“无法导入名称 'cygrpc'”的问题。 For 2.7, I get some unknown error with the .path file.对于 2.7,.path 文件出现一些未知错误。 I am following the AWS Lambda Deployment Package instructions here<\/a> .我在这里<\/a>遵循 AWS Lambda 部署包说明。 They recommend two options, and both do not work \/ result in the same issue.他们推荐了两个选项,两者都不起作用\/导致相同的问题。 Is GCP just not compatible with AWS Lambda for some reason? GCP 是否出于某种原因与 AWS Lambda 不兼容? What's the deal?这是怎么回事?

Neither Python 3.6 nor 2.7 work for me. Python 3.6 和 2.7 都不适合我。

NOTE: I am posting this question here to answer it myself because it took me quite a while to find a solution, and I would like to share my solution.注意:我在这里发布这个问题是为了自己回答,因为我花了很长时间才找到解决方案,我想分享我的解决方案。

"

TL;DR: You cannot compile the deployment package on your Mac or whatever pc you use. TL;DR:您无法在 Mac 或您使用的任何 PC 上编译部署包。 You have to do it using a specific OS/"setup", the same one that AWS Lambda uses to run your code.您必须使用特定的操作系统/“设置”来执行此操作,这与 AWS Lambda 用于运行您的代码的操作系统相同。 To do this, you have to use EC2.为此,您必须使用 EC2。

I will provide here an answer on how to get Google Cloud Vision working on AWS Lambda for Python 2.7.我将在此处提供有关如何让 Google Cloud Vision 在适用于 Python 2.7 的 AWS Lambda 上工作的答案。 This answer is potentially extendable for other other APIs and other programming languages on AWS Lambda.此答案可能适用于 AWS Lambda 上的其他 API 和其他编程语言。

So the my journey to a solution began with this initial posting on Github with others who have the same issue.所以我的解决方案之旅始于在 Github 上与其他有同样问题的人的 最初发布 One solution someone posted was有人发布的一种解决方案是

I had the same issue " cannot import name 'cygrpc' " while running the lambda.我在运行 lambda 时遇到了同样的问题“无法导入名称‘cygrpc’”。 Solved it with pip install google-cloud-vision in the AMI amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2 instance and exported the lib/python3.6/site-packages to aws lambda Thank you @tseaver在 AMI amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2 实例中使用 pip install google-cloud-vision 解决了这个问题,并将 lib/python3.6/site-packages 导出到 aws lambda 谢谢@tseaver

This is partially correct, unless I read it wrong, but regardless it led me on the right path.这是部分正确的,除非我读错了,但无论如何它使我走上了正确的道路。 You will have to use EC2.您将不得不使用 EC2。 Here are the steps I took:以下是我采取的步骤:

  1. Set up an EC2 instance by going to EC2 on Amazon.通过转至 Amazon 上的 EC2 来设置 EC2 实例。 Do a quick read about AWS EC2 if you have not already.如果您还没有,请快速阅读一下 AWS EC2。 Set one up for amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2 or something along those lines (ie the most updated one).为 amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2 或类似的东西设置一个(即最新的)。
  2. Get your EC2 .pem file.获取您的 EC2 .pem 文件。 Go to your Terminal.前往您的终端。 cd into your folder where your .pem file is. cd 进入您的 .pem 文件所在的文件夹。 ssh into your instance using使用 ssh 进入您的实例

    ssh -i "your-file-name-here.pem" ec2-user@ec2-ip-address-here.compute-1.amazonaws.com ssh -i "你的文件名-here.pem" ec2-user@ec2-ip-address-here.compute-1.amazonaws.com

  3. Create the following folders on your instance using mkdir: google-cloud-vision, protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2.使用 mkdir 在您的实例上创建以下文件夹:google-cloud-vision、protobuf、google-api-python-client、httplib2、uritemplate、google-auth-httplib2。

  4. On your EC2 instance, cd into google-cloud-vision.在您的 EC2 实例上,cd 进入 google-cloud-vision。 Run the command:运行命令:

    pip install google-cloud-vision -t . pip 安装 google-cloud-vision -t 。

Note If you get "bash: pip: command not found", then enter "sudo easy_install pip" source .注意如果您收到“bash: pip: command not found”,请输入“sudo easy_install pip” source

  1. Repeat step 4 with the following packages, while cd'ing into the respective folder: protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2.对以下包重复步骤 4,同时 cd 进入相应的文件夹:protobuf、google-api-python-client、httplib2、uritemplate、google-auth-httplib2。

  2. Copy each folder on your computer.复制计算机上的每个文件夹。 You can do this using the scp command.您可以使用 scp 命令执行此操作。 Again, in your Terminal, not your EC2 instance and not the Terminal window you used to access your EC2 instance, run the command (below is an example for your "google-cloud-vision" folder, but repeat this with every folder):同样,在您的终端,而不是您的 EC2 实例和您用来访问 EC2 实例的终端窗口中,运行命令(以下是您的“google-cloud-vision”文件夹的示例,但对每个文件夹重复此操作):

    sudo scp -r -i your-pem-file-name.pem ec2-user@ec2-ip-address-here.compute-1.amazonaws.com:~/google-cloud-vision ~/Documents/your-local-directory/ sudo scp -r -i your-pem-file-name.pem ec2-user@ec2-ip-address-here.compute-1.amazonaws.com:~/google-cloud-vision ~/Documents/your-local-目录/

  3. Stop your EC2 instance from the AWS console so you don't get overcharged.从 AWS 控制台停止您的 EC2 实例,这样您就不会被多收费用。

  4. For your deployment package, you will need a single folder containing all your modules and your Python scripts.对于您的部署包,您将需要一个包含所有模块和 Python 脚本的文件夹。 To begin combining all of the modules, create an empty folder titled "modules."要开始组合所有模块,请创建一个名为“modules”的空文件夹。 Copy and paste all of the contents of the "google-cloud-vision" folder into the "modules" folder.将“google-cloud-vision”文件夹的所有内容复制并粘贴到“modules”文件夹中。 Now place only the folder titled "protobuf" from the "protobuf" (sic) main folder in the "Google" folder of the "modules" folder.现在只将“protobuf”(原文如此)主文件夹中名为“protobuf”的文件夹放入“modules”文件夹的“Google”文件夹中。 Also from the "protobuf" main folder, paste the Protobuf .pth file and the -info folder in the Google folder.同样从“protobuf”主文件夹中,将 Protobuf .pth 文件和 -info 文件夹粘贴到 Google 文件夹中。

  5. For each module after protobuf, copy and paste in the "modules" folder the folder titled with the module name, the .pth file, and the "-info" folder.对于 protobuf 之后的每个模块,将标题为模块名称的文件夹、.pth 文件和“-info”文件夹复制并粘贴到“模块”文件夹中。

  6. You now have all of your modules properly combined (almost).您现在已经正确组合了所有模块(几乎)。 To finish combination, remove these two files from your "modules" folder: googleapis_common_protos-1.5.3-nspkg.pth and google_cloud_vision-0.34.0-py3.6-nspkg.pth.要完成组合,请从“模块”文件夹中删除这两个文件:googleapis_common_protos-1.5.3-nspkg.pth 和 google_cloud_vision-0.34.0-py3.6-nspkg.pth。 Copy and paste everything in the "modules" folder into your deployment package folder.将“modules”文件夹中的所有内容复制并粘贴到您的部署包文件夹中。 Also, if you're using GCP, paste in your .json file for your credentials as well.此外,如果您使用 GCP,请粘贴您的 .json 文件作为您的凭据。

  7. Finally, put your Python scripts in this folder, zip the contents (not the folder), upload to S3, and paste the link in your AWS Lambda function and get going!最后,将您的 Python 脚本放在此文件夹中,压缩内容(而不是文件夹),上传到 S3,然后将链接粘贴到您的 AWS Lambda 函数中,然后开始吧!

If something here doesn't work as described, please forgive me and either message me or feel free to edit my answer.如果这里的某些内容与描述的不一样,请原谅我并给我发消息或随时编辑我的答案。 Hope this helps.希望这可以帮助。

Building off the answer from @Josh Wolff (thanks a lot, btw!), this can be streamlined a bit by using a Docker image for Lambdas that Amazon makes available.基于@Josh Wolff 的答案(非常感谢,顺便说一句!),这可以通过使用亚马逊提供的 Lambda 的 Docker 镜像来简化。

You can either bundle the libraries with your project source or, as I did below in a Makefile script, upload it as an AWS layer.您可以将这些库与您的项目源捆绑在一起,或者像我在下面的Makefile脚本中所做的那样,将其作为 AWS 层上传。

layer:
    set -e ;\
    docker run -v "$(PWD)/src":/var/task "lambci/lambda:build-python3.6" /bin/sh -c "rm -R python; pip install -r requirements.txt -t python/lib/python3.6/site-packages/; exit" ;\
    pushd src ;\
    zip -r my_lambda_layer.zip python > /dev/null ;\
    rm -R python ;\
    aws lambda publish-layer-version --layer-name my_lambda_layer --description "Lambda layer" --zip-file fileb://my_lambda_layer.zip --compatible-runtimes "python3.6" ;\
    rm my_lambda_layer.zip ;\
    popd ;

The above script will:上面的脚本将:

  1. Pull the Docker image if you don't have it yet (above uses Python 3.6)如果您还没有 Docker 镜像,请拉取它(以上使用 Python 3.6)
  2. Delete the python directory (only useful for running a second time)删除python目录(只对第二次运行有用)
  3. Install all requirements to the python directory, created in your projects /src directory将所有需求安装到python目录中,在你的项目 /src 目录中创建
  4. ZIP the python directory压缩python目录
  5. Upload the AWS layer上传 AWS 层
  6. Delete the python directory and zip file删除python目录和zip文件

Make sure your requirements.txt file includes the modules listed above by Josh: google-cloud-vision, protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2确保您的requirements.txt文件包含 Josh 上面列出的模块:google-cloud-vision、protobuf、google-api-python-client、httplib2、uritemplate、google-auth-httplib2

There's a fast solution that doesn't require much coding.有一个不需要太多编码的快速解决方案。

Cloud9 uses AMI so using pip on their virtual environment should make it work. Cloud9 使用 AMI,因此在他们的虚拟环境中使用 pip 应该可以使其工作。

I created a Lambda from the Cloud9 UI and from the console activated the venv for the EC2 machine.我从 Cloud9 UI 创建了一个 Lambda,并从控制台为 EC2 机器激活了 venv。 I proceeded to install google-cloud-speech with pip.That was enough to fix the issue.我继续使用 pip 安装 google-cloud-speech。这足以解决问题。

I was facing same error using goolge-ads API.我在使用 goolge-ads API 时遇到了同样的错误。

{ "errorMessage": "Unable to import module 'lambda_function': cannot import name'cygrpc' from 'grpc._cython' (\/var\/task\/grpc\/_cython\/ init<\/strong> .py)","errorType": "Runtime.ImportModuleError","stackTrace": []} {“errorMessage”:“无法导入模块'lambda_function':无法从'grpc._cython'(\/var\/task\/grpc\/_cython\/ init<\/strong> .py)导入名称'cygrpc'”,“errorType”:“Runtime.ImportModuleError “,“堆栈跟踪”: []}

<\/blockquote>

My Lambda runtime was Python 3.9 and architecture x86_64.我的 Lambda 运行时是 Python 3.9 和架构 x86_64。

If somebody encounter similar ImportModuleError then see my answer here : Cannot import name 'cygrpc' from 'grpc._cython' - Google Ads API<\/a>如果有人遇到类似的 ImportModuleError,请在此处查看我的答案: Cannot import name 'cygrpc' from 'grpc._cython' - Google Ads API<\/a>

"

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS Lambda to Firestore错误:无法导入名称&#39;cygrpc&#39; - AWS Lambda to Firestore error: cannot import name 'cygrpc' AWS lambda无法导入名称'_bcrypt' - AWS lambda cannot import name '_bcrypt' AWS Lambda 无法导入模块“演示”:无法导入名称“windll” - AWS Lambda Unable to import module 'demo': cannot import name 'windll' AWS Lambda Python3.7 函数 - numpy:无法导入名称“WinDLL” - AWS Lambda Python3.7 Function - numpy: cannot import name 'WinDLL' AWS Lambda python 错误:Runtime.ImportModuleError:无法导入模块“app”:无法从“pyparsing”导入名称“operatorPrecedence” - AWS Lambda python Error: Runtime.ImportModuleError: Unable to import module 'app': cannot import name 'operatorPrecedence' from 'pyparsing' 谷歌云 bigquery 导入失败,因为“导入错误:无法从‘google.api’导入名称‘client_pb2’” - google cloud bigquery import failing because "ImportError: cannot import name 'client_pb2' from 'google.api'" Google Cloud Platform 到 AWS 的迁移 [PostgreSQL 9.6] - Google Cloud Platform to AWS migration [PostgreSQL 9.6] 无法在 aws lambda 函数中导入图层 - Cannot import layers in aws lambda function 用于通过 REST API 更新 Google Cloud Platform 中 Cloud Armor 中的规则的 Python 脚本 - The Python script for updating rules in the Cloud Armor in Google Cloud Platform by REST API AWS python lambda 中的导入重试模块 - Import retry module in AWS python lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM