我有一个CodePipline,它从CodeCommit中获取代码,将其捆绑在CodeBuild中,然后通过CloudFormation发布。
我想使用 Python 包 gspread,因为它不是标准 AWS Linux 映像的一部分,我需要安装它。
目前,当代码运行时,我收到错误:
- [错误] 运行时导入模块错误:无法导入模块"索引":没有名为"gspread"的模块
代码结构
- buildspec.yml
- template.yml
package/
- gspread/
- gspread-3.6.0.dist-info/
- (37 other python packages)
source/
- index.py
buildspec.yml -- 已编辑
version: 0.2
phases:
install:
commands:
# Use Install phase to install packages or any pre-reqs you may need throughout the build (e.g. dev deps, security checks, etc.)
- echo "[Install phase]"
- pip install --upgrade pip
- pip install --upgrade aws-sam-cli
- sam --version
- cd source
- ls
- pip install --target . gspread oauth2client
# consider using pipenv to install everything in the environement and then copy the files installed into the /source folder
- ls
runtime-versions:
python: 3.8
pre_build:
commands:
# Use Pre-Build phase to run tests, install any code deps or any other customization before build
# - echo "[Pre-Build phase]"
build:
commands:
- cd ..
- sam build
post_build:
commands:
# Use Post Build for notifications, git tags and any further customization after build
- echo "[Post-Build phase]"
- export BUCKET=property-tax-invoice-publisher-deployment
- sam package --template-file template.yml --s3-bucket $BUCKET --output-template-file outputtemplate.yml
- echo "SAM packaging completed on `date`"
##################################
# Build Artifacts to be uploaded #
##################################
artifacts:
files:
- outputtemplate.yml
discard-paths: yes
cache:
paths:
# List of path that CodeBuild will upload to S3 Bucket and use in subsequent runs to speed up Builds
- '/root/.cache/pip'
index.py 文件中的内容远不止于此。但要显示冒犯的台词。
--index.py --
import os
import boto3
import io
import sys
import csv
import json
import smtplib
import gspread #**<--- Right here**
def lambda_handler(event, context):
print("In lambda_handler")
我尝试过什么
- 创建/package 文件夹并提交 gspread 和其他软件包
- 在CodeBuild版本中运行"pip install gspread":命令:
目前,我正在到处安装它,看看有什么坚持。(目前没有任何粘连
(版本: 蟒蛇 3.8
我认为您可能需要执行以下步骤:
-
使用虚拟环境在本地安装软件包。
-
创建需求.txt让代码生成知道包要求。
-
在 CodeBuild 构建规范中.xml 包含用于安装 virutal env 的命令,然后提供 requirements.txt。
pre_build: 命令:
- pip install virtualenv
- Virtualenv env
- . env/bin/activate
- 点安装 -r 要求.txt
详细步骤在这里供参考:
https://adrian.tengamnuay.me/programming/2018/07/01/continuous-deployment-with-aws-lambda/