gpt4 book ai didi

amazon-web-services - 通过 CodeBuild 在 AWS Lambda 上安装 NLTK/WORDNET

转载 作者:行者123 更新时间:2023-12-04 08:14:57 26 4
gpt4 key购买 nike

我正在尝试通过 CodeBuild 让 NLTK 和 Wordnet 在 lambda 上运行。

看起来它在 CloudFormation 中安装得很好,但我在 Lambda 中收到以下错误:

START RequestId: c660c446-e1c4-11e8-8047-15f59f1e002c Version: $LATEST
Unable to import module 'index': No module named 'nltk'

END RequestId: c660c446-e1c4-11e8-8047-15f59f1e002c
REPORT RequestId: c660c446-e1c4-11e8-8047-15f59f1e002c Duration: 2.10 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 21 MB

然而,当我检查时,它在 CodeBuild 中安装正常:

[Container] 2018/11/06 12:45:06 Running command pip install -U nltk
Collecting nltk
Downloading https://files.pythonhosted.org/packages/50/09/3b1755d528ad9156ee7243d52aa5cd2b809ef053a0f31b53d92853dd653a/nltk-3.3.0.zip (1.4MB)
Requirement already up-to-date: six in /usr/local/lib/python2.7/site-packages (from nltk)
Building wheels for collected packages: nltk
Running setup.py bdist_wheel for nltk: started
Running setup.py bdist_wheel for nltk: finished with status 'done'
Stored in directory: /root/.cache/pip/wheels/d1/ab/40/3bceea46922767e42986aef7606a600538ca80de6062dc266c
Successfully built nltk
Installing collected packages: nltk
Successfully installed nltk-3.3

这是实际的 python 代码:

import json
import datetime
import nltk
from nltk.corpus import wordnet as wn

这是 YML 文件:

version: 0.2

phases:
install:
commands:

# Upgrade AWS CLI to the latest version
- pip install --upgrade awscli

# Install nltk & WordNet
- pip install -U nltk
- python -m nltk.downloader wordnet

pre_build:
commands:

# Discover and run unit tests in the 'tests' directory. For more information, see <https://docs.python.org/3/library/unittest.html#test-discovery>
# - python -m unittest discover tests

build:
commands:

# Use AWS SAM to package the application by using AWS CloudFormation
- aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml

artifacts:
type: zip
files:
- template-export.yml

知道为什么它在 CodeBuild 中安装良好但无法在 Lambda 中访问模块 NLTK 吗?作为引用,如果您只是删除 NLTK,代码在 lambda 中运行良好。

我感觉这是一个 YML 文件问题,但不确定是什么,因为 NLTK 安装正常。

最佳答案

NLTK 仅在本地安装在运行 CodeBuild 作业的机器上。您需要将 NLTK 复制到 CloudFormation 部署包中。您的 buildspec.yml 将如下所示:

install:
commands:

# Upgrade AWS CLI to the latest version
- pip install --upgrade awscli

pre_build:
commands:
- virtualenv /venv

# Install nltk & WordNet
- pip install -U nltk
- python -m nltk.downloader wordnet

build:
commands:
- cp -r /venv/lib/python3.6/site-packages/. ./

# Use AWS SAM to package the application by using AWS CloudFormation
- aws cloudformation package --template template.yml --s3-bucket $S3_BUCKET --output-template template-export.yml

补充阅读:

关于amazon-web-services - 通过 CodeBuild 在 AWS Lambda 上安装 NLTK/WORDNET,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53172633/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com