首页 > 解决方案 > 将 Flask 应用程序部署到 Google Cloud Platform 时无法获得正确的 TensorFlow 版本

问题描述

我正在尝试将 Python Flask 应用程序部署到 Google Cloud Platform (App Engine)。该应用程序在我的计算机上本地运行良好。我在我的应用程序中使用 TensorFlow 2.3.0 版。当我尝试将应用程序部署到 Google Cloud Platform 时,这似乎是一个问题。尝试构建应用程序时,我在构建日志中收到此错误代码:

Collecting tensorflow==2.3.0 (from -r requirements.txt (line 2))
Step #1:   Could not find a version that satisfies the requirement tensorflow==2.3.0 (from -r requirements.txt (line 2)) (from versions: 0.12.1, 1.0.0, 1.0.1, 1.1.0rc0, 1.1.0rc1, 1.1.0rc2, 1.1.0
, 1.2.0rc0, 1.2.0rc1, 1.2.0rc2, 1.2.0, 1.2.1, 1.3.0rc0, 1.3.0rc1, 1.3.0rc2, 1.3.0, 1.4.0rc0, 1.4.0rc1, 1.4.0, 1.4.1, 1.5.0rc0, 1.5.0rc1, 1.5.0, 1.5.1, 1.6.0rc0, 1.6.0rc1, 1.6.0, 1.7.0rc0, 1.7.0r
c1, 1.7.0, 1.7.1, 1.8.0rc0, 1.8.0rc1, 1.8.0, 1.9.0rc0, 1.9.0rc1, 1.9.0rc2, 1.9.0, 1.10.0rc0, 1.10.0rc1, 1.10.0, 1.10.1, 1.11.0rc0, 1.11.0rc1, 1.11.0rc2, 1.11.0, 1.12.0rc0, 1.12.0rc1, 1.12.0rc2, 
1.12.0, 1.12.2, 1.12.3, 1.13.0rc0, 1.13.0rc1, 1.13.0rc2, 1.13.1, 1.13.2, 1.14.0rc0, 1.14.0rc1, 1.14.0, 2.0.0a0, 2.0.0b0, 2.0.0b1)
Step #1: No matching distribution found for tensorflow==2.3.0 (from -r requirements.txt (line 2))
Step #1: You are using pip version 10.0.1, however version 20.2.3 is available.
Step #1: You should consider upgrading via the 'pip insll --upgrade pip' command.
Step #1: The command '/bin/sh -c pip install -r requirements.txt' returned a tanon-zero code: 1
Finished Step #1
ERROR
ERROR: build step 1 "gcr.io/cloud-builders/docker@sha256:c1128bf8a32cfe4f2efcb920551a71a43d3401d3a3631f8b745e90b75099b68e" failed: step exited with non-zero status: 1
Step #1: 

在构建日志的早些时候,我得到了这个:

Step 3/9 : RUN virtualenv --no-download /env -p python3.6
Step #1:  ---> Running in cd3673a277e1
Step #1: Running virtualenv with interpreter /opt/python3.6/bin/python3.6
Step #1: Using base prefix '/opt/python3.6'
Step #1: New python executable in /env/bin/python3.6
Step #1: Also creating executable in /env/bin/python
Step #1: Installing setuptools, pip, wheel...done.
Step #1: Removing intermediate container cd3673a277e1

如您所见,该应用程序使用 Python 3.6 环境。如何指定我想在 Python 3.7 或 3.8 环境中构建我的应用程序?

我的需求文件如下所示:

pip==20.2.3
tensorflow==2.3.0
Flask==0.11.1
gunicorn==19.5.0
numpy==1.17.4
scipy==1.4.1
h5py==2.10.0

我的 YAML 文件如下所示:

entrypoint: "gunicorn -b:$PORT main:app"
env: flex
runtime: python
runtime_config: 
  python_version: 3

从错误代码中,我猜问题是:为什么找不到Tensorflow 2.3.0?它与旧的python版本或旧的pip版本有关吗?将应用程序部署到 Google Cloud Platform 时,如何安装更新的 pip 和 Python 版本?

在部署应用程序之前,我尝试在 Google Cloud Shell 中更新 pip。然后我收到了这个警告:

********************************************************************************
Python 2 is deprecated. Upgrade to Python 3 as soon as possible.
See https://cloud.google.com/python/docs/python2-sunset
To suppress this warning, create an empty ~/.cloudshell/no-python-warning file.
The command will automatically proceed in  seconds or on any key.
********************************************************************************
DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2
021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Defaulting to user installation because normal site-packages is not writeable
Requirement already up-to-date: pip in /home/bjorn_sing/.local/lib/python2.7/site-packages (20.2.3)
bjorn_sing@cloudshell:~/FlaskApp (my-first-flask-app-289008)$ pip3 install --upgrade pip
Cache entry deserialization failed, entry ignored
Collecting pip
  Using cached https://files.pythonhosted.org/packages/4e/5f/528232275f6509b1fff703c9280e58951a81abe24640905de621c9f81839/pip-20.2.3-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-20.2.3

标签: pythontensorflowgoogle-cloud-platformpipyaml

解决方案


我在我自己的环境中复制了您的设置,并具有与您相同的错误消息,据我所知,问题似乎与 pip 版本有关,在 Python3.6 运行时中可能也很困难。

需要注意的重要一点是,当使用 App Engine 提供的默认运行时时,配置程度是有限的,这有时会导致跨库版本等不兼容。例如,App Engine Flexible 中的 Python 运行时仍然不支持 Python3.8,而 Standard App Engine 支持。

也就是说,保持灵活环境的一种解决方案是通过提供您自己的 Dockerfile 来使用自定义运行时,您可以在其中指定您想要的任何基本映像和运行时。这是我为此用例测试的示例 Dockerfile 和 app.yaml:

# Dockerfile
FROM python:3.8-slim

ENV APP_HOME /app
WORKDIR $APP_HOME
COPY . ./

RUN pip install --upgrade pip
RUN pip install --no-cache-dir -r requirements.txt

CMD exec gunicorn --bind :$PORT --workers 1 --threads 8 --timeout 0 main:app

-------------------

# app.yaml
runtime: custom
env: flex

也就是说,我还必须指出,shakhyar 的答案是不正确的。虽然您确实可以启用调试模式并对持有灵活实例的虚拟机进行 SSH 连接,但所做的所有更改都是临时的,一旦禁用调试模式就会丢失。此外,必须为每个实例重复此过程,并且在调试模式下关闭所有安全补丁。因此,连接到您的实例并不能解决问题。文档在这里


推荐阅读