从AWS Lambda / tmp目录导入python包

时间:2018-07-23 04:54:31

标签: python amazon-web-services aws-lambda

我正在尝试将一个大小为300MB的zip文件(主要包含python软件包)上传到AWS Lambda。我清楚地了解,如果我们直接使用AWS开发工具包上传,则超出了可以上传到Lambda的zip的限制。因此,这将不起作用。

为了克服这个问题,我决定将软件包下载到cout << endl;目录中,并将其导入到主文件中(参考文献here)。我将所需的软件包压缩为for并将其上传到AWS S3。然后我使用int main() { int n, t1 = 0, t2 = 1,t3 = 2, nextTerm = 0; cout << "Enter the number of terms: "; cin >> n; cout << "Fibonacci number sequence up to " << n << ":" << endl; for (int i = 1; i <= n+1; i++) { if (i == 1) { cout << " " << t1; continue; } if (i == 2) { cout << " " << t2 << " "; continue; } nextTerm = t1 + t2; t1 = t2; t2 = nextTerm; cout << nextTerm << ""; } cout << endl; cout << "Number of times iterative function is called: " << n * 3 << endl; return 0; 下载它们,将它们提取到/tmp

pkgs.zip

但是,当我测试lambda函数时,它返回一个错误,提示找不到requests

/tmp/

我的问题是,如何从def get_pkgs(url): import requests import io import zipfile print("Getting Packages...") re = requests.get(url) z = zipfile.ZipFile(io.BytesIO(re.content)) print("Extracting Packages...") z.extractall("/tmp/") print("Packages are downloaded and extracted.") def attempt_import(): print("="*50) print("ATTEMPT TO IMPORT DEPENDENCIES...") print("="*50) import numpy import scipy import six print("IMPORTING DONE.") def main(): URL = "https://s3-ap-southeast-1.amazonaws.com/BUCKET_NAME/pkgs.zip" get_pkgs(URL) attempt_import() def lambda_handler(event, context): main() return "Hello Lambda" 目录中导入所需的软件包?

谢谢。

1 个答案:

答案 0 :(得分:0)

在将任何已下载的软件包导入到/tmp文件夹(例如,到/tmp/requirements文件夹)之前,必须告诉系统在那寻找依赖项。在代码的开头,仅在以下几行:

import sys

sys.path.insert(0, '/tmp/requirements/') # Or any path you desire