• <i id='CGLL5'><tr id='CGLL5'><dt id='CGLL5'><q id='CGLL5'><span id='CGLL5'><b id='CGLL5'><form id='CGLL5'><ins id='CGLL5'></ins><ul id='CGLL5'></ul><sub id='CGLL5'></sub></form><legend id='CGLL5'></legend><bdo id='CGLL5'><pre id='CGLL5'><center id='CGLL5'></center></pre></bdo></b><th id='CGLL5'></th></span></q></dt></tr></i><div id='CGLL5'><tfoot id='CGLL5'></tfoot><dl id='CGLL5'><fieldset id='CGLL5'></fieldset></dl></div>

        <bdo id='CGLL5'></bdo><ul id='CGLL5'></ul>

        <small id='CGLL5'></small><noframes id='CGLL5'>

      1. <legend id='CGLL5'><style id='CGLL5'><dir id='CGLL5'><q id='CGLL5'></q></dir></style></legend>
        <tfoot id='CGLL5'></tfoot>

        在 AWS lambda 中创建 utils.py

        create utils.py in AWS lambda(在 AWS lambda 中创建 utils.py)

        • <small id='eBffI'></small><noframes id='eBffI'>

                <tbody id='eBffI'></tbody>
                <legend id='eBffI'><style id='eBffI'><dir id='eBffI'><q id='eBffI'></q></dir></style></legend>
              • <tfoot id='eBffI'></tfoot>

                <i id='eBffI'><tr id='eBffI'><dt id='eBffI'><q id='eBffI'><span id='eBffI'><b id='eBffI'><form id='eBffI'><ins id='eBffI'></ins><ul id='eBffI'></ul><sub id='eBffI'></sub></form><legend id='eBffI'></legend><bdo id='eBffI'><pre id='eBffI'><center id='eBffI'></center></pre></bdo></b><th id='eBffI'></th></span></q></dt></tr></i><div id='eBffI'><tfoot id='eBffI'></tfoot><dl id='eBffI'><fieldset id='eBffI'></fieldset></dl></div>

                  <bdo id='eBffI'></bdo><ul id='eBffI'></ul>

                  本文介绍了在 AWS lambda 中创建 utils.py的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我的 home/file.py 文件中有一个 def hello() 函数.我创建了一个 home/common/utils.py 文件并将函数移到那里.现在,我想将它导入我的文件 file.py.

                  I had a def hello() function in my home/file.py file. I created a home/common/utils.pyfile and moved the function there. Now, I want to import it in my file file.py.

                  我是这样导入的: from utils import hellofrom common.utils import hello 并且我的文件中的导入不会引发错误.但是,当我在 AWS Lambda 上运行它时,我收到一个错误:

                  I imported it like this: from utils import hello and from common.utils import hello and the import in my file doesn't throw an error. However, when I run it on AWS Lambda, I get an error that:

                  Runtime.ImportModuleError:无法导入模块文件":没有名为utils"的模块

                  我该如何解决这个问题?无需使用 Ec2 之类的...

                  How can I fix this? without having to use Ec2 or something...

                  data "archive_file" "file_zip" {
                    type             = "zip"
                    source_file      = "${path.module}/src/file.py"
                    output_file_mode = "0666"
                    output_path      = "${path.module}/bin/file.zip"
                  }
                  

                  推荐答案

                  您上传的部署包仅包含您的主要 Python 脚本 (file.py).具体来说,它不包含任何依赖项,例如 common/utils.py.这就是代码在 Lambda 中运行时导入失败的原因.

                  The deployment package that you're uploading only contains your main Python script (file.py). Specifically, it does not include any dependencies such as common/utils.py. That's why the import fails when the code runs in Lambda.

                  修改您的创建部署包(file.zip),使其包含所有需要的依赖项.

                  Modify the creation of your deployment package (file.zip) so that it includes all needed dependencies.

                  例如:

                  data "archive_file" "file_zip" {
                    type             = "zip"
                    output_file_mode = "0666"
                    output_path      = "${path.module}/bin/file.zip"
                  
                    source {
                      content  = file("${path.module}/src/file.py")
                      filename = "file.py"
                    }
                  
                    source {
                      content  = file("${path.module}/src/common/utils.py")
                      filename = "common/utils.py"
                    }
                  }
                  

                  如果您的所有文件碰巧都在一个文件夹中,那么您可以使用 source_dir 而不是指示单个文件.

                  If all of your files happen to be in a single folder then you can use source_dir instead of indicating the individual files.

                  注意:我不使用 Terraform,所以带有嵌入插值的 file(...) 可能不是 100% 正确,但你明白了.

                  Note: I don't use Terraform so the file(...) with embedded interpolation may not be 100% correct, but you get the idea.

                  这篇关于在 AWS lambda 中创建 utils.py的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  env: python: No such file or directory(env: python: 没有这样的文件或目录)
                  How to evaluate environment variables into a string in Python?(如何在 Python 中将环境变量评估为字符串?)
                  Python - temporarily modify the current process#39;s environment(Python - 临时修改当前进程的环境)
                  Change current process environment#39;s LD_LIBRARY_PATH(更改当前进程环境的 LD_LIBRARY_PATH)
                  Reading and writing environment variables in Python?(在 Python 中读写环境变量?)
                  When to use sys.path.append and when modifying %PYTHONPATH% is enough(何时使用 sys.path.append 以及何时修改 %PYTHONPATH% 就足够了)

                    • <bdo id='fPQy1'></bdo><ul id='fPQy1'></ul>

                      <small id='fPQy1'></small><noframes id='fPQy1'>

                        <i id='fPQy1'><tr id='fPQy1'><dt id='fPQy1'><q id='fPQy1'><span id='fPQy1'><b id='fPQy1'><form id='fPQy1'><ins id='fPQy1'></ins><ul id='fPQy1'></ul><sub id='fPQy1'></sub></form><legend id='fPQy1'></legend><bdo id='fPQy1'><pre id='fPQy1'><center id='fPQy1'></center></pre></bdo></b><th id='fPQy1'></th></span></q></dt></tr></i><div id='fPQy1'><tfoot id='fPQy1'></tfoot><dl id='fPQy1'><fieldset id='fPQy1'></fieldset></dl></div>
                        <legend id='fPQy1'><style id='fPQy1'><dir id='fPQy1'><q id='fPQy1'></q></dir></style></legend>

                          <tbody id='fPQy1'></tbody>

                          • <tfoot id='fPQy1'></tfoot>