无法在 AWS Lambda 上使用 Requests-Module [英] Cannot use Requests-Module on AWS Lambda

查看:28
本文介绍了无法在 AWS Lambda 上使用 Requests-Module的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在每天运行一次的 python 脚本中进行一次休息调用.我无法使用 AWS Lambdas 将请求"包打包到我的 python 包中.我收到错误消息:无法导入模块‘lambda_function’:没有名为 lambda_function 的模块"

I need to do a rest-call within a python script, that runs once per day. I can't pack the "requests" package into my python-package using the AWS Lambdas. I get the error: "Unable to import module 'lambda_function': No module named lambda_function"

我将其分解为 hello_world 预定义脚本.我可以将其打包成 zip 并上传.一切正常.一旦我将导入请求"放入文件中,我就会收到此错误.

I broke it down to the hello_world predefined script. I can pack it into a zip and upload it. Everything works. As soon as I put "import requests" into the file, I get this error.

这是我已经做过的:

  1. zip 和项目文件夹(包括子文件夹)的权限设置为 `chmod 777`.所以权限应该不成问题.
  2. 脚本本身位于根文件夹中.当您打开 zip 文件时,您可以直接看到它.
  3. 我使用 `sudo pip install requests -t PATH_TO_ROOT_FOLDER` 将请求包安装到项目的根文件夹中

一切的命名看起来像这样:

The naming of everything looks like this:

  • 压缩文件:lambda_function.zip
  • py 文件:lambda_function.py
  • 处理方法:lambda_handler(event, context)
  • webconfig: lambda_function.lambda_handler"中的处理程序定义

我最后要运行的文件是这样的:

The file I want to run in the end looks like this:

import requests
import json


def lambda_handler(event, context):
    url = 'xxx.elasticbeanstalk.com/users/login'
    headers = {"content-type": "application/json", "Authorization": "Basic Zxxxxxxxxx3NjxxZxxxxzcw==" }
    response = requests.put(url, headers=headers, verify=False)
    return 'hello lambda_handler'

我很高兴得到任何形式的帮助.我已经在这个问题上花费了几个小时.

I'm glad for ANY kind of help. I already used multiple hours on this issue.

推荐答案

我终于解决了这个问题:我的 zip 文件中的结构已损坏.重要的是,python 脚本和打包的依赖项(作为文件夹)位于 zip 文件的根目录中.这解决了我的问题.

I finally solved the problem: The structure in my zip file was broken. It is important that the python script and the packed dependencies (as folders) are in the root of the zip file. This solved my problem.

如果您在数小时的尝试和失败后发现如此简单的错误,那会有点令人沮丧.

It's a bit depressing if you find such easy errors after hours of try and failure.

这篇关于无法在 AWS Lambda 上使用 Requests-Module的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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