如何在 python 中部署函数及其依赖项?
How do I deploy a function in python with its dependencies?
我正在尝试使用 serverless
框架来创建和部署 AWS Lambda 函数。我在项目的根目录中创建了一个名为 vendored
的文件夹并安装(使用 pip install
)函数依赖项。但是,每当我尝试 运行 它(使用 serverless function run
)时,我都会收到错误消息:
Serverless: Running isNewUser...
Serverless: WARNING: This variable is not defined: region
Serverless: -----------------
Serverless: Failed - This Error Was Returned:
Serverless: {"exception": ["Traceback (most recent call last):\n", " File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler\", line 170, in \n handler = import_program_as_module(path)\n", " File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler\", line 149, in import_program_as_module\n module = make_module_from_file('lambda_handler', handler_file)\n", " File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler\", line 129, in make_module_from_file\n py_source_description\n", " File \"/home/fernando/workspace/os-cac/isNewUser/handler.py\", line 11, in \n from vtex.order import Order\n", "ImportError: No module named vtex.order\n"], "success": false}
Serverless: Exception message from Python
Serverless: Traceback (most recent call last):
, File "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler", line 170, in
handler = import_program_as_module(path)
, File "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler", line 149, in import_program_as_module
module = make_module_from_file('lambda_handler', handler_file)
, File "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler", line 129, in make_module_from_file
py_source_description
, File "/home/fernando/workspace/os-cac/isNewUser/handler.py", line 11, in
from vtex.order import Order
,ImportError: No module named vtex.order
`
vtex.order
是在 handler.py
中导入的模块
我的项目结构是这样的:
/root/
|
|--_meta/
|--vendored/
|--dependencies...
|--function-name/
|--handler.py
|--event.json
|--s-function.json
|--requirements.txt
|--admin.env
|--package.json
|--s-project.json
|--s-resources-cf.json
|--s-project.json
我做错了什么吗?我是否应该以某种方式配置我的函数以包含 vendored 的依赖项?
这里有几个步骤可以让它发挥作用:
- 确保
s-function.json
中的处理程序条目在其路径中包含函数名称:
"handler": "function-name/handler.handler",
在handler.py
中添加以下内容:
import os
import sys
here = os.path.dirname(os.path.realpath(__file__))
sys.path.append(os.path.join(here, "../vendored"))
from vtex.order import Order
就是这样。让我知道它是否有效。
我按照以下步骤使用依赖项进行部署:
在项目根目录中为依赖项创建了一个目录mkdir .vendor
在requirements.txt文件中手动添加依赖项或使用pip freeze > requirements.txt
更新serverless.yml文件
package:
include:
- .vendor/**
- 将 .vendor 目录包含到 handler.py 文件中的系统路径
import sys
sys.path.insert(0, './.vendor')
- 安装依赖项
pip install -r requirements.txt -t .vendor
现在serverless deploy
将上传带有依赖项的函数。
我建议使用 serverless-python-requirements 插件来包含通过 pip
安装的软件包
我正在尝试使用 serverless
框架来创建和部署 AWS Lambda 函数。我在项目的根目录中创建了一个名为 vendored
的文件夹并安装(使用 pip install
)函数依赖项。但是,每当我尝试 运行 它(使用 serverless function run
)时,我都会收到错误消息:
Serverless: Running isNewUser...
Serverless: WARNING: This variable is not defined: region
Serverless: -----------------
Serverless: Failed - This Error Was Returned:
Serverless: {"exception": ["Traceback (most recent call last):\n", " File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler\", line 170, in \n handler = import_program_as_module(path)\n", " File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler\", line 149, in import_program_as_module\n module = make_module_from_file('lambda_handler', handler_file)\n", " File \"/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler\", line 129, in make_module_from_file\n py_source_description\n", " File \"/home/fernando/workspace/os-cac/isNewUser/handler.py\", line 11, in \n from vtex.order import Order\n", "ImportError: No module named vtex.order\n"], "success": false} Serverless: Exception message from Python
Serverless: Traceback (most recent call last): , File "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler", line 170, in handler = import_program_as_module(path) , File "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler", line 149, in import_program_as_module module = make_module_from_file('lambda_handler', handler_file) , File "/home/fernando/.nvm/versions/node/v5.10.1/bin/serverless-run-python-handler", line 129, in make_module_from_file py_source_description , File "/home/fernando/workspace/os-cac/isNewUser/handler.py", line 11, in from vtex.order import Order ,ImportError: No module named vtex.order `
vtex.order
是在 handler.py
我的项目结构是这样的:
/root/
|
|--_meta/
|--vendored/
|--dependencies...
|--function-name/
|--handler.py
|--event.json
|--s-function.json
|--requirements.txt
|--admin.env
|--package.json
|--s-project.json
|--s-resources-cf.json
|--s-project.json
我做错了什么吗?我是否应该以某种方式配置我的函数以包含 vendored 的依赖项?
这里有几个步骤可以让它发挥作用:
- 确保
s-function.json
中的处理程序条目在其路径中包含函数名称:"handler": "function-name/handler.handler",
在
handler.py
中添加以下内容:import os import sys here = os.path.dirname(os.path.realpath(__file__)) sys.path.append(os.path.join(here, "../vendored")) from vtex.order import Order
就是这样。让我知道它是否有效。
我按照以下步骤使用依赖项进行部署:
在项目根目录中为依赖项创建了一个目录
mkdir .vendor
在requirements.txt文件中手动添加依赖项或使用
pip freeze > requirements.txt
更新serverless.yml文件
package:
include:
- .vendor/**
- 将 .vendor 目录包含到 handler.py 文件中的系统路径
import sys
sys.path.insert(0, './.vendor')
- 安装依赖项
pip install -r requirements.txt -t .vendor
现在serverless deploy
将上传带有依赖项的函数。
我建议使用 serverless-python-requirements 插件来包含通过 pip