<p>根据您收到的错误,Cloud Build未能安装requirements.txt文件中指定的最后两个依赖项。如果转到<a href="https://cloud.google.com/cloud-build/docs/view-build-results#viewing_build_results" rel="nofollow noreferrer">Cloud Build History page</a>并检查失败的部署,您会注意到部署失败,并显示与此类似的错误消息:</p>
<pre><code>Step #1 - "builder": INFO `pip_download_wheels` had stderr output:
Step #1 - "builder": Could not find a version that satisfies the requirement openfoodfacts==0.1.0 (from -r requirements.txt (line 10)) (from versions: )
Step #1 - "builder": No matching distribution found for openfoodfacts==0.1.0 (from -r requirements.txt (line 10))
</code></pre>
<p>不需要在requirements.txt中包含<a href="https://docs.python.org/3.7/library/os.html?highlight=os#module-os" rel="nofollow noreferrer">os module</a>,因为它是Python 3.7标准库的一部分。例如,请注意,基于应用程序的<a href="https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/standard_python37/building-an-app/building-an-app-1/main.py" rel="nofollow noreferrer">main.py</a>导入了<a href="https://docs.python.org/3.7/library/datetime.html?highlight=datetime#module-datetime" rel="nofollow noreferrer">datetime module</a>,而这并没有在<a href="https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/appengine/standard_python37/building-an-app/building-an-app-1/requirements.txt" rel="nofollow noreferrer">requirements.txt file</a>上指定</p>
<p><a href="https://github.com/openfoodfacts/openfoodfacts-python" rel="nofollow noreferrer">Open Food Facts package</a>实际上还没有在PyPI上发布(即,<a href="https://pypi.org/project/openfoodfacts/" rel="nofollow noreferrer">https://pypi.org/project/openfoodfacts/</a>是404),因此您需要参考<a href="https://cloud.google.com/appengine/docs/standard/python3/specifying-dependencies#private_dependencies" rel="nofollow noreferrer">App Engine's documentation</a>的相关部分并手动添加它,方式与此类似:</p>
<ol>
<li>在应用程序的项目目录上创建<code>lib</code>目录(例如,my using <code>mkdir lib</code>)</李>
<li>通过发出以下命令将所需的依赖项复制到新创建的<code>lib</code>目录:<code>pip3 install git+git://github.com/openfoodfacts/openfoodfacts-python.git -t lib</code></李>
<li>转到<code>lib</code>目录并验证是否已正确安装所有依赖项</李>
<li>修改<code>requirements.txt</code>文件以使用Open Food Facts包所需的依赖项。应该是这样的:</li>
</ol>
<pre><code>Flask==1.1.1
flask-wtf==0.14.2
unidecode==1.1.1
numpy==1.18.1
certifi==2019.11.28
chardet==3.0.4
idna==2.8
requests==2.22.0
urllib3==1.25.8
</code></pre>
<ol start=“5”>
<li>以类似于<code>import lib.openfoodfacts as openff</code>的方式在应用程序中导入模块</李>
<li>使用<code>gcloud app deploy</code>命令部署应用程序</李>
</ol>
<p>最后,您应该有一个文件结构与此类似的项目目录:</p>
<pre><code>building-an-app-1/
app.yaml
lib/
...
openfoodfacts/
autosuggest.py
facets.py
__init__.py
openbeautyfacts.py
openpetfoodfacts.py
products.py
__pycache__/
utils.py
...
main.py
requirements.txt
static/
script.js
style.css
templates/
index.html
</code></pre>
<p>其中,您可以使用以下命令导入<code>main.py</code>文件上的<code>os</code>和<code>openfoodfacts</code>模块:</p>
<pre><code>...
import datetime
import os
import lib.openfoodfacts as openff
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
....
</code></pre>