【问题标题】:Unbale to build a python app with AWS SAM无法使用 AWS SAM 构建 Python 应用程序
【发布时间】:2021-09-27 00:09:30
【问题描述】:

我正在尝试从 AWS SAM 构建和运行示例 python 应用程序。我刚刚安装了python,下面是命令行给出的..

D:\Udemy Work>python
Python 3.9.6 (tags/v3.9.6:db3ff76, Jun 28 2021, 15:26:21) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>>

D:\Udemy Work>pip -V
pip 21.1.3 from c:\users\user\appdata\local\programs\python\python39\lib\site-packages\pip (python 3.9)

当我运行sam build 时,出现以下错误

Build Failed
Error: PythonPipBuilder:Validation - Binary validation failed for python, searched for python in following locations  : ['C:\\Users\\User\\AppData\\Local\\Programs\\Python\\Python39\\python.EXE', 'C:\\Users\\User\\AppData\\Local\\Microsoft\\WindowsApps\\python.EXE'] which did not satisfy constraints for runtime: python3.8. Do you have python for runtime: python3.8 on your PATH?

下面是我的代码

template.yaml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  python-test

  Sample SAM Template for python-test

# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
Globals:
  Function:
    Timeout: 3

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
    Properties:
      CodeUri: hello_world/
      Handler: app.lambda_handler
      Runtime: python3.8
      Events:
        HelloWorld:
          Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
          Properties:
            Path: /hello
            Method: get

app.py

AWSTemplateFormatVersion: '2010-09-09'
    Transform: AWS::Serverless-2016-10-31
    Description: >
      python-test
    
      Sample SAM Template for python-test
    
    # More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst
    Globals:
      Function:
        Timeout: 3
    
    Resources:
      HelloWorldFunction:
        Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
        Properties:
          CodeUri: hello_world/
          Handler: app.lambda_handler
          Runtime: python3.9
          Events:
            HelloWorld:
              Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
              Properties:
                Path: /hello
                Method: get

如果我在 yaml 中更改运行时间,则会收到以下错误

PS D:\Udemy Work\awslambda\python-test> sam build
Building codeuri: D:\Udemy Work\awslambda\python-test\hello_world runtime: python3.9 metadata: {} functions: ['HelloWorldFunction']

Build Failed
Error: 'python3.9' runtime is not supported

这里有什么解决办法?

【问题讨论】:

    标签: python amazon-web-services aws-lambda amazon-cloudformation aws-sam


    【解决方案1】:

    python3.9不支持。支持的运行时在here 中列出,您可以使用的最新python 是python3.8

    SAM supports docker 通过 --use-container 标志。因此,您可以使用它为您想要 SAM 的任何 python 版本构建您的包。

    【讨论】:

    • 感谢您的帮助。如何告诉 AWS SAM 使用 3.8?
    • @JustCause 我认为你必须安装 python 3.8。我不确定您是否可以强制 sam 使用您在操作系统中安装的不同版本。
    • 谢谢马尔辛。我是 Python 新手。卸载当前版本后安装?或者只是安装和python会解决它?
    • @JustCause 我会考虑使用 docker 或 vm。在操作系统级别安装新的 python 版本可能会弄乱其他软件。但这取决于你的操作系统。
    • 明白。我正在使用微软。
    【解决方案2】:

    还要确保将 SAM CLI 更新到最新版本以支持 Python 3.9 运行时。

    【讨论】:

    猜你喜欢
    • 2020-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-14
    • 1970-01-01
    • 2019-02-27
    • 2020-10-23
    • 1970-01-01
    相关资源
    最近更新 更多