简体   繁体   English

无服务器 aws python lambda 无法从父目录导入模块

[英]serverless aws python lambda unable to import module from parent directory

I have following structure我有以下结构

在此处输入图片说明

I am trying to import utils inside handler.py我正在尝试在 handler.py 中导入 utils

I tried giving like我试着给

from src.utils import *
from utils import *

also tried adding也尝试添加

import os
import sys

sys.path.append(os.path.dirname(os.path.realpath(__file__)))

and

import os
    import sys
    
    sys.path.append('src')

but nothing working但没有任何效果

I am using serverless framework not manually uploading the zip file我使用的是无服务器框架,没有手动上传 zip 文件

Tried alot but nothing working...尝试了很多但没有任何效果......

Any help, highly appreciated.任何帮助,高度赞赏。

Thanks谢谢

You can install serverless-functions-base-path plugin and use.您可以安装serverless-functions-base-path插件并使用。

Check out the guide on the plugin here https://www.serverless.com/plugins/serverless-functions-base-path在此处查看插件指南https://www.serverless.com/plugins/serverless-functions-base-path

If your serverless.yml file and src in the directory, you can add this to you serverless.yml file and try deploy如果您的 serverless.yml 文件和 src 在目录中,您可以将其添加到您的 serverless.yml 文件中并尝试部署

custom:
  functionsBasePath: src

plugins:
  - serverless-functions-base-path

You can use serverless-python-requirements plugin for this.您可以为此使用 serverless-python-requirements 插件。

It can be installed locally or on a pipeline with它可以安装在本地或管道上

sls plugin install -n serverless-python-requirements 

You can add this to you serverless.yml file and try deploy您可以将其添加到 serverless.yml 文件中并尝试部署

# this part might not be needed depending on size of utils
custom:
  pythonRequirements:
    zip: true

# This plugin allows us import dependencies
plugins:
  - serverless-python-requirements

Check out the guide on the plugin here在此处查看插件指南

https://www.serverless.com/blog/serverless-python-packaging https://www.serverless.com/blog/serverless-python-packaging

https://www.npmjs.com/package/serverless-python-requirementshttps://www.npmjs.com/package/serverless-python-requirements

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM