本文介绍了Python Serverless (SLS):Runtime.ImportModuleError:无法导入模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个项目,该项目使用 AWS CodeBuild 部署用 Python 编写的无服务器 (SLS) 函数.

I am working on a project that is using AWS CodeBuild to deploy a Serverless (SLS) function that is written in Python.

部署在代码构建中运行良好.它成功创建了函数,我可以在 Lambda AWS UI 中查看 lambda.每当触发该功能时,我都会收到如下所示的错误:

The deployment works fine within code build. It successfully creates the function and I can view the lambda within the Lambda AWS UI. Whenever the function is triggered, I get the error seen below:

Runtime.ImportModuleError: Unable to import module 'some/function': attempted relative import with no known parent package

这非常令人沮丧,因为我知道该函数存在于上面列出的目录中.在 CodeBuild 脚本期间,我可以 ls 进入目录并确认它确实存在.该函数在我的 serverless.yml 文件中定义如下:

It is extremely frustrating as I know the function exists at that directory listed above. During the CodeBuild script, I can ls into the directory and confirm that it indeed exists. The function is defined in my serverless.yml file as follows:

functions:
  file-blaster:
    runtime: python3.7
    handler: some/function.function_name
    events:
      - existingS3:
          bucket: some_bucket
          events:
            - s3:ObjectCreated:*
          rules:
            - prefix: ${opt:stage}/some/prefix

遗憾的是,我一直无法破解这个.有没有人在云端使用 SLS 和 python 时有过类似的经历?

Sadly, I haven't been able to crack this one. Has anyone had a similar experience while working with SLS and python in the cloud?

SLS 能够成功构建和部署似乎很奇怪,但 Lambda 本身找不到该函数.

It seems odd that SLS would build and deploy successfully, but the Lambda itself cant find the function.

推荐答案

这将是关于 Python 导入的较长讨论的简短回答.作为 Python 项目的设计,您可以自己研究相对导入和绝对导入之间的激烈而混乱的斗争.

This will be a short answer for what is a somewhat longer discussion on Python imports. You can do the research yourself on the hectic and confusing battle between relative and absolute imports as a design for a python project.

要点:有必要了解 SLS 函数的 Python 导入基础 IS serverless.yml 文件所在的位置(我想它类似于具有 main.py 调用在 sls yml 中被称为函数"的其他文件).对于我上面的情况,当我遇到问题时,我没有使用绝对导入来构建导入.我将所有导入都切换为绝对路径,因此当我移动包时,它会继续工作.

The Gist:It is necessary to understand that the base of the python importing for SLS functions IS where the serverless.yml file exists (I imagine that it is similar to having a main.py that calls the other files that are referenced as "functions" in the sls yml). For my case above, I did not structure the imports using absolute imports when I had my issues. I switched all of my imports to have absolute paths, so when I moved the package around, it would continue to work.

给我的错误 Runtime.ImportModuleError: Unable to import module 'some/function':试图相对导入没有已知的父包 描述实际问题真的很糟糕.错误应该包括在尝试相对导入时找不到 some/function 正在使用的包,因为这是需要修复的实际问题.

The error that I was given Runtime.ImportModuleError: Unable to import module 'some/function': attempted relative import with no known parent package was really poor to describe the actual issue. The error should have included that the packages being used by some/function were not found when attempting a relative import because that was the actual problem that needed fixing.

希望有一天这能帮助其他人.如果我可以提供更多信息,我还没有提供,请告诉我.

Hopefully this helps someone else out someday. Let me know if I can provide more information where I haven't already.

这篇关于Python Serverless (SLS):Runtime.ImportModuleError:无法导入模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 07:27