无法让 AWS SAM CLI 使用 Python 3.5.x

Cannot get AWS SAM CLI to work with Python 3.5.x

我确定这个问题是由于我自己对 Python 和 pip 的无知,但是,我已经在网上搜索并尝试了很多方法来使它起作用,到目前为止,都无济于事。

我有一个基于 Bitbucket 管道默认图像(版本 2)的 docker 图像:atlassian/default-image:2,它使用 Python 2.7.x。 SAM 不适用,我查看了 AWS 的文档,上面说 SAM CLI 不再支持 Python 2.7.x,所以我从中删除了所有 Python 安装docker 容器(在本地调试 Bitbucket 管道)并尝试确保只安装了 Python3 和 pip3(我什至设置了从 /usr/bin/python/usr/bin/python3 和从/usr/bin/pip/usr/bin/pip3 - 虽然,这似乎是一件必须要做的蠢事。

我还尝试使用 Python 3.6 执行上述操作,因为错误消息(如下所示)似乎显示 Python 'f-String' 语法,根据 Python的文档是在3.6中引入的。

我所做的一切似乎都不起作用,而且我越看这个,我就越被束缚在一个看似永无止境的 Python 版本和 linux 包的网络中!

错误(和 python 版本)可以在这里看到:

root@eb3dc6ff9997:/localDebugRepo/infrastructure# pip3 install -U aws-sam-cli==1.22.0
Requirement already up-to-date: aws-sam-cli==1.22.0 in /usr/local/lib/python3.5/dist-packages
Requirement already up-to-date: jmespath~=0.10.0 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: boto3~=1.14 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: cookiecutter~=1.7.2 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: aws-sam-translator==1.35.0 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: aws-lambda-builders==1.3.0 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: tomlkit==0.7.0 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: dateparser~=0.7 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: requests==2.23.0 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: watchdog==0.10.3 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: PyYAML~=5.3 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: serverlessrepo==0.1.10 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: Flask~=1.1.2 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: chevron~=0.12 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: click~=7.1 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: docker~=4.2.0 in /usr/local/lib/python3.5/dist-packages (from aws-sam-cli==1.22.0)
Requirement already up-to-date: s3transfer<0.4.0,>=0.3.0 in /usr/local/lib/python3.5/dist-packages (from boto3~=1.14->aws-sam-cli==1.22.0)
Requirement already up-to-date: botocore<1.21.0,>=1.20.50 in /usr/local/lib/python3.5/dist-packages (from boto3~=1.14->aws-sam-cli==1.22.0)
Requirement already up-to-date: binaryornot>=0.4.4 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: six>=1.10 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: poyo>=0.5.0 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: jinja2-time>=0.2.0 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: MarkupSafe<2.0.0 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: Jinja2<3.0.0 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: python-slugify>=4.0.0 in /usr/local/lib/python3.5/dist-packages (from cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: jsonschema~=3.2 in /usr/local/lib/python3.5/dist-packages (from aws-sam-translator==1.35.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: setuptools in /usr/local/lib/python3.5/dist-packages (from aws-lambda-builders==1.3.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: wheel in /usr/local/lib/python3.5/dist-packages (from aws-lambda-builders==1.3.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: regex!=2019.02.19 in /usr/local/lib/python3.5/dist-packages (from dateparser~=0.7->aws-sam-cli==1.22.0)
Requirement already up-to-date: pytz in /usr/local/lib/python3.5/dist-packages (from dateparser~=0.7->aws-sam-cli==1.22.0)
Requirement already up-to-date: tzlocal in /usr/local/lib/python3.5/dist-packages (from dateparser~=0.7->aws-sam-cli==1.22.0)
Requirement already up-to-date: python-dateutil in /usr/local/lib/python3.5/dist-packages (from dateparser~=0.7->aws-sam-cli==1.22.0)
Requirement already up-to-date: chardet<4,>=3.0.2 in /usr/local/lib/python3.5/dist-packages (from requests==2.23.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: idna<3,>=2.5 in /usr/local/lib/python3.5/dist-packages (from requests==2.23.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.5/dist-packages (from requests==2.23.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: certifi>=2017.4.17 in /usr/local/lib/python3.5/dist-packages (from requests==2.23.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: pathtools>=0.1.1 in /usr/local/lib/python3.5/dist-packages (from watchdog==0.10.3->aws-sam-cli==1.22.0)
Requirement already up-to-date: itsdangerous>=0.24 in /usr/local/lib/python3.5/dist-packages (from Flask~=1.1.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: Werkzeug>=0.15 in /usr/local/lib/python3.5/dist-packages (from Flask~=1.1.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: websocket-client>=0.32.0 in /usr/local/lib/python3.5/dist-packages (from docker~=4.2.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: arrow in /usr/local/lib/python3.5/dist-packages (from jinja2-time>=0.2.0->cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: text-unidecode>=1.3 in /usr/local/lib/python3.5/dist-packages (from python-slugify>=4.0.0->cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: importlib-metadata; python_version < "3.8" in /usr/local/lib/python3.5/dist-packages (from jsonschema~=3.2->aws-sam-translator==1.35.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: pyrsistent>=0.14.0 in /usr/local/lib/python3.5/dist-packages (from jsonschema~=3.2->aws-sam-translator==1.35.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: attrs>=17.4.0 in /usr/local/lib/python3.5/dist-packages (from jsonschema~=3.2->aws-sam-translator==1.35.0->aws-sam-cli==1.22.0)
Requirement already up-to-date: typing-extensions; python_version < "3.8" in /usr/local/lib/python3.5/dist-packages (from arrow->jinja2-time>=0.2.0->cookiecutter~=1.7.2->aws-sam-cli==1.22.0)
Requirement already up-to-date: zipp>=0.5 in /usr/local/lib/python3.5/dist-packages (from importlib-metadata; python_version < "3.8"->jsonschema~=3.2->aws-sam-translator==1.35.0->aws-sam-cli==1.22.0)
You are using pip version 8.1.1, however version 21.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
root@eb3dc6ff9997:/localDebugRepo/infrastructure# python --version
bash: /usr/bin/python: No such file or directory
root@eb3dc6ff9997:/localDebugRepo/infrastructure# pip --version
bash: /usr/bin/pip: No such file or directory
root@eb3dc6ff9997:/localDebugRepo/infrastructure# python3 --version
Python 3.5.2
root@eb3dc6ff9997:/localDebugRepo/infrastructure# pip3 --version
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
root@eb3dc6ff9997:/localDebugRepo/infrastructure# sam --version
Traceback (most recent call last):
  File "/usr/local/bin/sam", line 7, in <module>
    from samcli.cli.main import cli
  File "/usr/local/lib/python3.5/dist-packages/samcli/cli/main.py", line 86
    cmdline_args_log += f"--{key} "
                                  ^
SyntaxError: invalid syntax
root@eb3dc6ff9997:/localDebugRepo/infrastructure# 

真的希望有一些理智的方法可以准确地知道我需要哪个 Python 版本以及安装和配置它的理智方法。

请帮助我:)

在大多数情况下,与其修改现有映像中的 python 安装或安装新映像,我建议只使用已经具有 python 3 的 docker 映像安装。 This bitbucket page 谈论使用不同的基础图像。

还有一个page about using a python base image spectifically

除此之外,根据您希望如何使用 SAM,您也许可以使用 builtin Bitbucket "pipe" for deploying to SAM instead. Here is some info on pipes.。我认为这将绕过在您的构建中安装 SAM 的需要。

我自己没有使用过那个特定的管道,但我认为它的工作方式是在管道中构建部署工件文件,缓存它以便在以后的步骤中可用,然后将该文件设置为输入SAM 管道来部署它。 SAM 管道在单独的容器中运行,因此它已安装在该容器中。你只要传入文件即可。

最后,我从未使用过 SAM,所以对此持保留态度,但我没有在 the official AWS Linux installation docs or on the github page. I did find this issue 上看到使用 pip 安装 CLI 的参考,从中我收集了以下内容:

  • pip可以安装但不推荐
  • Python aws sam cli 不支持 3.5
  • 如果您使用说明中的安装程序,听起来它会为您处理 python 版本控制。
  • 他们想要针对 Python 的单个版本,因此如果您使用 pip,也许最新的或第二个最新的 Python 版本可以工作。只是猜测...