如何解决将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环境中构建我的App?
我的需求文件如下:
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中的点。然后我得到了这个警告:
********************************************************************************
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
解决方法
我在自己的环境中重现了您的设置,并显示了与您相同的错误消息,并且据我所知,问题似乎与pip版本有关,可能与Python3.6运行时中的问题有关。
要注意的一件事是,当使用App Engine提供的默认运行时时,配置程度受到限制,这有时会导致跨库版本等的不兼容。例如,App Engine Flexible中的Python运行时仍然不支持Python3.8,而Standard App Engine则支持。
也就是说,与“灵活环境”配合使用的解决方案是通过提供自己的Dockerfile使用custom runtime,在其中您可以指定想要的任何基本映像和运行时。这是我为该用例测试的示例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托管拥有灵活实例的VM,所有所做的更改都是临时的,并且在禁用调试模式后将立即丢失。此外,必须对每个实例都重复此过程,并且在调试模式下必须关闭所有安全补丁程序。因此,连接到实例将无法解决问题。文档here