适用于Python和AWS Lambda的Google Cloud Platform API不兼容:无法导入名称"cygrpc" [英] Google Cloud Platform API for Python and AWS Lambda Incompatibility: Cannot import name 'cygrpc'

查看:206
本文介绍了适用于Python和AWS Lambda的Google Cloud Platform API不兼容:无法导入名称"cygrpc"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将适用于Python的Google Cloud Platform(特别是Vision API)与AWS Lambda结合使用.因此,我必须为我的依赖项创建一个部署程序包.但是,当我尝试创建此部署程序包时,无论使用哪个Python版本(3.6或2.7),都会遇到一些编译错误.考虑到版本3.6,出现问题无法导入名称'cygrpc'".对于2.7,.path文件出现一些未知错误.我正在遵循AWS Lambda部署程序包说明

I am trying to use Google Cloud Platform (specifically, the Vision API) for Python with 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). Considering the version 3.6, I get the issue "Cannot import name 'cygrpc'". For 2.7, I get some unknown error with the .path file. I am following the AWS Lambda Deployment Package instructions here. 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? What's the deal?

Python 3.6和2.7都不适合我.

Neither Python 3.6 nor 2.7 work for me.

注意:我将在此处发布此问题以自行回答,因为我花了相当长的时间才能找到解决方案,并且我想分享我的解决方案.

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:您无法在Mac或任何使用的PC上编译部署程序包.您必须使用特定的OS/设置"来完成它,AWS Lambda用来运行您的代码.为此,您必须使用EC2.

TL;DR: You cannot compile the deployment package on your Mac or whatever pc you use. You have to do it using a specific OS/"setup", the same one that AWS Lambda uses to run your code. To do this, you have to use EC2.

我将在此处提供有关如何使Google Cloud Vision在适用于Python 2.7的AWS Lambda上运行的答案.该答案可能可扩展到AWS Lambda上的其他其他API和其他编程语言.

I will provide here an answer on how to get Google Cloud Vision working on AWS Lambda for Python 2.7. This answer is potentially extendable for other other APIs and other programming languages on AWS Lambda.

因此,我寻求解决方案的旅程始于此初始发布在Github上与有同样问题的其他人一起.有人发布的一种解决方案是

So the my journey to a solution began with this initial posting on Github with others who have the same issue. One solution someone posted was

我在运行时遇到了同样的问题无法导入名称'cygrpc'" lambda.使用AMI中的pip install google-cloud-vision解决了它 amzn-ami-hvm-2017.03.1.20170812-x86_64-gp2实例并导出 lib/python3.6/site-packages到aws lambda谢谢@tseaver

I had the same issue " cannot import name 'cygrpc' " while running the lambda. 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

这是部分正确的,除非我读错了,但无论它使我走上了正确的道路.您将必须使用EC2.这是我采取的步骤:

This is partially correct, unless I read it wrong, but regardless it led me on the right path. You will have to use EC2. Here are the steps I took:

  1. 通过转到Amazon上的EC2来设置EC2实例.如果还没有,请快速阅读有关AWS EC2的信息.为amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2或类似的东西设置一个(即最新的).
  2. 获取您的EC2 .pem文件.转到您的终端. cd到您的.pem文件所在的文件夹中.使用

  1. Set up an EC2 instance by going to EC2 on Amazon. Do a quick read about AWS EC2 if you have not already. Set one up for amzn-ami-hvm-2018.03.0.20180811-x86_64-gp2 or something along those lines (i.e. the most updated one).
  2. Get your EC2 .pem file. Go to your Terminal. cd into your folder where your .pem file is. ssh into your instance using

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

ssh -i "your-file-name-here.pem" ec2-user@ec2-ip-address-here.compute-1.amazonaws.com

使用mkdir在实例上创建以下文件夹:google-cloud-vision,protobuf,google-api-python-client,httplib2,uritemplate,google-auth-httplib2.

Create the following folders on your instance using mkdir: google-cloud-vision, protobuf, google-api-python-client, httplib2, uritemplate, google-auth-httplib2.

在您的EC2实例上,CD进入google-cloud-vision.运行命令:

On your EC2 instance, cd into google-cloud-vision. Run the command:

pip安装google-cloud-vision -t.

pip install google-cloud-vision -t .

注意:如果收到"bash:pip:找不到命令",请输入"sudo easy_install pip"

Note If you get "bash: pip: command not found", then enter "sudo easy_install pip" source.

  1. 使用以下软件包重复第4步,同时将其压缩到相应的文件夹中:protobuf,google-api-python-client,httplib2,uritemplate,google-auth-httplib2.

  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.

复制计算机上的每个文件夹.您可以使用scp命令执行此操作.同样,在您的终端中,而不是您的EC2实例以及您用来访问EC2实例的终端窗口中,运行命令(以下是"google-cloud-vision"文件夹的示例,但请对每个文件夹重复此操作):

Copy each folder on your computer. You can do this using the scp command. 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):

sudo scp -r -i your-pem-file-name.pem ec2-user@ec2-ip-address-here.compute-1.amazonaws.com:〜/google-cloud-vision〜/Documents/您的-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-directory/

从AWS控制台停止EC2实例,这样就不会多收费用了.

Stop your EC2 instance from the AWS console so you don't get overcharged.

对于您的部署软件包,您将需要一个包含所有模块和Python脚本的文件夹.要开始组合所有模块,请创建一个名为模块"的空文件夹.复制"google-cloud-vision"文件夹的所有内容并将其粘贴到"modules"文件夹中.现在,仅将"protobuf"(原文如此)主文件夹中名为"protobuf"的文件夹放置在模块"文件夹的"Google"文件夹中.同样从"protobuf"主文件夹中,将Protobuf .pth文件和-info文件夹粘贴到Google文件夹中.

For your deployment package, you will need a single folder containing all your modules and your Python scripts. To begin combining all of the modules, create an empty folder titled "modules." Copy and paste all of the contents of the "google-cloud-vision" folder into the "modules" folder. Now place only the folder titled "protobuf" from the "protobuf" (sic) main folder in the "Google" folder of the "modules" folder. Also from the "protobuf" main folder, paste the Protobuf .pth file and the -info folder in the Google folder.

对于protobuf之后的每个模块,将标题为模块名称,.pth文件和"-info"文件夹的文件夹复制并粘贴到"modules"文件夹中.

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.

现在,您已正确组合了所有模块(几乎).要完成合并,请从模块"文件夹中删除以下两个文件:googleapis_common_protos-1.5.3-nspkg.pth和google_cloud_vision-0.34.0-py3.6-nspkg.pth.将模块"文件夹中的所有内容复制并粘贴到部署包文件夹中.另外,如果您使用的是GCP,请同时粘贴.json文件中的凭据.

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. Copy and paste everything in the "modules" folder into your deployment package folder. Also, if you're using GCP, paste in your .json file for your credentials as well.

最后,将您的Python脚本放在此文件夹中,压缩内容(不是文件夹),上传到S3,然后将链接粘贴到您的AWS Lambda函数中,然后开始!

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!

如果此处的内容无法按照说明进行操作,请原谅我,或者给我发消息,也可以随时编辑我的答案.希望这会有所帮助.

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.

这篇关于适用于Python和AWS Lambda的Google Cloud Platform API不兼容:无法导入名称"cygrpc"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆