首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过CodeBuild在AWS上安装NLTK/WORDNET

通过CodeBuild在AWS上安装NLTK/WORDNET
EN

Stack Overflow用户
提问于 2018-11-06 13:12:40
回答 2查看 746关注 0票数 3

我试图让NLTK和Wordnet通过CodeBuild在lambda上工作。

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

代码语言:javascript
复制
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中安装得很好:

代码语言:javascript
复制
[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代码:

代码语言:javascript
复制
import json
import datetime
import nltk
from nltk.corpus import wordnet as wn

这里是YML文件:

代码语言:javascript
复制
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安装得很好。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-07 11:26:53

好吧,谢谢莱卡给我指明了正确的方向。

这是一个通过CodeStar / CodeBuild将NLTK & Wordnet部署到Lambda的工作部署。有些事情要记住:

1)您不能使用source venv/bin/activate,因为它不符合POSIX。用下面的. venv/bin/activate代替。

2)必须设置NLTK的路径,如“定义目录”部分所示。

buildspec.yml

代码语言:javascript
复制
version: 0.2

phases:
  install:
    commands:

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

      # Define Directories
      - export HOME_DIR=`pwd`
      - export NLTK_DATA=$HOME_DIR/nltk_data

  pre_build:
    commands:
      - cd $HOME_DIR

      # Create VirtualEnv to package for lambda
      - virtualenv venv
      - . venv/bin/activate

      # Install Supporting Libraries
      - pip install -U requests

      # Install WordNet
      - pip install -U nltk
      - python -m nltk.downloader -d $NLTK_DATA wordnet

      # Output Requirements
      - pip freeze > requirements.txt

      # Unit Tests
      # - python -m unittest discover tests

  build:
    commands:
      - cd $HOME_DIR
      - mv $VIRTUAL_ENV/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

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

如果有人有任何改进,LMK。对我来说很管用。

票数 2
EN

Stack Overflow用户

发布于 2018-11-06 15:59:58

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

代码语言:javascript
复制
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

补充案文:

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53172633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档