【问题标题】:Can't get the right TensorFlow version when deploying a Flask app to Google Cloud Platform将 Flask 应用程序部署到 Google Cloud Platform 时无法获得正确的 TensorFlow 版本
【发布时间】:2020-12-28 15:39:15
【问题描述】:

我正在尝试将 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

【问题讨论】:

  • 看起来 tensorflow 2.3.x 无法安装在您的云平台中。升级你的点子python -m pip install --upgrade pip。如果 pip 仍然找不到 tensorflow 2.3.0,那么使用这个:tensorflow==2.0.0b1,因为这看起来像是谷歌云平台上当前 pip 的最新版本。
  • 我在哪里写python -m pip install --upgrade pip ?在requirement.txt 或app.yaml 中?还是我把它写在别的地方?
  • 通过使用云外壳。 Cloud Shell 在 Google Cloud Console 中打开的终端窗口中提供对虚拟机实例的命令行访问。您可以打开到同一个实例的多个 shell 连接。只需运行 gcloud --help 即可查看可供您使用的 gcloud 命令行工具命令。
  • 我尝试运行命令python -m pip install --upgrade pip,但似乎我正在运行 Python 2.7(请参阅上面的最后更新)
  • 当我运行 gcloud app deploy 之后,我收到了这条消息:pip version 10.0.1, however version 20.2.3 is available.

标签: python tensorflow google-cloud-platform pip yaml


【解决方案1】:

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

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

也就是说,保持灵活环境的一种解决方案是使用custom runtime,提供您自己的 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 连接持有灵活实例的虚拟机,但所做的所有更改都是临时的,一旦禁用调试模式就会丢失。此外,必须为每个实例重复此过程,并且在调试模式下关闭所有安全补丁。因此,连接到您的实例并不能解决问题。文档here

【讨论】:

  • 感谢@Happy-Monad 的非常好的回答!在做了一些研究之后,我想我会尝试使用 Heroku 来部署我的烧瓶应用程序。
猜你喜欢
  • 2019-04-24
  • 1970-01-01
  • 2018-07-16
  • 2018-08-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-30
  • 2019-10-06
相关资源
最近更新 更多