简体   繁体   English

无法在 AWS Lambda function 中导入外部 Python 库(tweepy)

[英]Can't import external Python library (tweepy) in AWS Lambda function

I'm a complete amateur with this kind of thing.我对这种事情完全是个业余爱好者。 I'm trying to move my little Twitter bot from Heroku to AWS Lambda, but Tweepy seems to be causing trouble.我正在尝试将我的小 Twitter 机器人从 Heroku 移动到 AWS Lambda,但 Tweepy 似乎正在引起麻烦。 I think the problem is just with importing the module at all, but here is everything in my lambda_function.py file, in case there's something else.我认为问题完全在于导入模块,但这是我的lambda_function.py文件中的所有内容,以防万一。

import tweepy
import random
import os
import json

ckey = os.getenv('ckey')
csec = os.getenv('csec')
atok = os.getenv('atok')
atos = os.getenv('atos')

def lambda_handler(event, context):
    # TODO implement
    tweet()
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }
    
def tweet():
    client = tweepy.Client(consumer_key=ckey, consumer_secret=csec, access_token=atok, access_token_secret=atos)
    quote = random.choice(list(open('quotes.txt')))
    client.create_tweet(text = quote)

I'm aware I need to upload any external packages as a Lambda layer, which I did do (and tried multiple different ways).我知道我需要上传任何外部包作为 Lambda 层,我确实这样做了(并尝试了多种不同的方式)。 The zip file I currently have uploaded as the layer for the function looks like this, in addition to a dist-info folder for each library's folder that I didn't include here.我目前上传的 zip 文件作为 function 的层看起来像这样,除了每个库的文件夹的 dist-info 文件夹,我没有在这里包括。

tweepylayer.zip
└───python
    └───python310 
        └───lib
             └───site-packages
                  └───bin
                    │ certifi
                    │ charset_normalizer
                    │ idna
                    │ oauthlib
                    │ requests
                    │ requests_oauthlib
                    │ tweepy
                    │ urllib3

Every time I attempt to test the function, it gives me this error.每次我尝试测试 function 时,都会出现此错误。

{
  "errorMessage": "Unable to import module 'lambda_function': No module named 'tweepy'",
  "errorType": "Runtime.ImportModuleError",
  "requestId": "1a06e902-f8b9-4c3b-b671-8934d4183dc3",
  "stackTrace": []
}

Thank you so, so much to anyone who is able to help at all, I'm still struggling to wrap my head around any of this stuff.非常感谢您,非常感谢任何能够提供帮助的人,我仍在努力解决这些问题。 And my apologies for this post being so long, I wasn't sure what did or did not need to be included.我很抱歉这篇文章太长了,我不确定什么需要或不需要包括在内。

This folder hierarchy is not supported by the Lambda Python runtime. Lambda Python 运行时不支持此文件夹层次结构。 The modules need to be installed under python directory or python/lib/python3.10/site-packages directory and zipped.模块需要安装在python目录或python/lib/python3.10/site-packages目录下并压缩。

More in AWS documentation . AWS 文档中的更多内容

tweepylayer.zip
└───python
    └───certifi
    └───charset_normalizer
    └───idna
    └───oauthlib
    └───requests
    └───requests_oauthlib
    └───tweepy
    └───urllib3

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM