【问题标题】:Can't Upload python script on Google Cloud Console. (Error response [13] && Error response [9])无法在 Google Cloud Console 上上传 python 脚本。 (错误响应 [13] && 错误响应 [9])
【发布时间】:2018-06-06 21:00:00
【问题描述】:

我昨天一直在尝试将我的脚本部署到 Google Cloud 控制台,但没有成功! Gcloud 版本:

Google Cloud SDK 204.0.0
beta 2017.09.15
bq 2.0.34
core 2018.06.04
gsutil 4.31

yaml 文件:

runtime: python
env: flex
entrypoint: gunicorn -b :$PORT main:app

runtime_config:
  python_version: 3
manual_scaling:
  instances: 1
resources:
  cpu: 1
  memory_gb: 1
  disk_size_gb: 10

requirements.txt 文件:

Flask==0.12.2
gunicorn==19.8.1

我尝试使用 gcloud beta app deploy 进行部署,但出现以下错误:

ERROR: (gcloud.app.deploy) Error Response: [9]
Application startup error:
[2018-06-06 09:28:47 +0000] [1] [INFO] Starting gunicorn 19.8.1
[2018-06-06 09:28:47 +0000] [1] [INFO] Listening at: http://0.0.0.0:8080 (1)
[2018-06-06 09:28:47 +0000] [1] [INFO] Using worker: sync
[2018-06-06 09:28:47 +0000] [7] [INFO] Booting worker with pid: 7
[2018-06-06 09:28:47 +0000] [7] [ERROR] Exception in worker process
Traceback (most recent call last):
  File "/env/lib/python3.6/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
    worker.init_process()
  File "/env/lib/python3.6/site-packages/gunicorn/workers/base.py", line 129, in init_process
    self.load_wsgi()
  File "/env/lib/python3.6/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
    self.wsgi = self.app.wsgi()
  File "/env/lib/python3.6/site-packages/gunicorn/app/base.py", line 67, in wsgi
    self.callable = self.load()
  File "/env/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
    return self.load_wsgiapp()
  File "/env/lib/python3.6/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
    return util.import_app(self.app_uri)
  File "/env/lib/python3.6/site-packages/gunicorn/util.py", line 350, in import_app
    __import__(module)
  File "/home/vmagent/app/main.py", line 5, in <module>
    import firebase_admin
ModuleNotFoundError: No module named 'firebase_admin'
[2018-06-06 09:28:47 +0000] [7] [INFO] Worker exiting (pid: 7)
[2018-06-06 09:28:47 +0000] [1] [INFO] Shutting down: Master
[2018-06-06 09:28:47 +0000] [1] [INFO] Reason: Worker failed to boot.

我还将所有模块都安装到虚拟环境中...

但是,我也尝试使用 gcloud app deploy 进行部署,但我得到了一个不同的错误 -> ERROR: (gcloud.beta.app.deploy) Error Response: [13] Error while processing files. 提到现在已关闭的类似问题 (https://github.com/GoogleCloudPlatform/getting-started-java/issues/281) 我做了这个命令 gcloud config set app/use_deprecated_preparation True,作为大多数的人说这是有效的,但对我来说不是!

我的 main.py 中的代码片段

# [START app]
import logging
import sys

import firebase_admin
import moment

from datetime import datetime
from firebase_admin import credentials, db
from flask import Flask, json, make_response, render_template, request

from apiclient.discovery import build, build_from_document
from firebase import firebase
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials

service_account = 'FIREBASE-ADMIN-SDK-KEY'
database_url = 'FIRABE-DB-URL'

cred = credentials.Certificate(service_account)

# Initialize the app with a service account, granting admin privileges
firebase_admin.initialize_app(cred, {
    'databaseURL': database_url
})

firebase = firebase.FirebaseApplication(database_url, None)

user_id = ""

rootDB = db.reference()
messagesDb = rootDB.child('messages')

app = Flask(__name__)

if sys.version_info.major < 3:
    reload(sys)
sys.setdefaultencoding('utf8')

scopes = ['https://www.googleapis.com/auth/chat.bot']

credentials = ServiceAccountCredentials.from_json_keyfile_name(
    'Google-Serviced-Account-KEY', scopes)

http_auth = credentials.authorize(Http())

chat = build('chat', 'v1', http=http_auth, cache_discovery=False)


# Using the rest api (send message function)
@app.route('/receiver', methods=['POST'])
def message_responses():

    #Some Code here

    return 'OK'

# [END message_response]


@app.route('/getMeasages', methods=['POST'])
def get_messages():

    # Some Code here

    return 'OK'

# [END get_messages]

@app.route('/test', methods=['POST'])
def resp_get():

    event_data = request.get_json()
    print(event_data)

    return 'OK'


@app.route('/', methods=['GET'])
def home_get():    

    return render_template('home.html')


if __name__ == '__main__':
    # This is used when running locally. Gunicorn is used to run the
    # application on Google App Engine. See entrypoint in app.yaml.
    app.run(host='localhost', port=8080, debug=True)
# [END app]

【问题讨论】:

  • 我认为您需要重新访问您的 requirements.txt 内容:我相当确定 Flaskgunicorn 都没有提供 firebase_admin - 您可能缺少依赖项。也许尝试这种方法来发现它们:stackoverflow.com/questions/50627798/…
  • 感谢@DanCornilescu 的提示,但即使在将所有依赖项添加到 requirements.txt 文件后,仍然出现相同的错误
  • 这确实是依赖问题。它在本地有效吗?您声明您已将所有模块安装到虚拟环境中。我刚刚通过执行以下操作进行了快速测试: $ virtualenv test $ source test/bin/activate $ pip freeze(确保没有安装任何内容) $ pip install firebase_admin 运行导入 firebase_admin 的 python 脚本。我还尝试通过在 requirements.txt 文件中添加“firebase-admin==2.11.0”来进行部署。部署时也没有问题。如果还是不行,能否给我们看看更新后的 requirements.txt 和 main.py 的 sn-p 代码?
  • 是的,这似乎是一个依赖问题。我将所有依赖项添加到我的要求中。 txt 文件。是的,它在本地运行,我只想托管它谷歌云控制台。我按照你说的做了,创建了一个新的 virtualenv 测试并且没有应用依赖项我 pip install firebase_admin 但仍然得到相同的错误
  • @VasilVasilev 你能分享 main.py 的 sn-p 代码吗?

标签: python google-app-engine deployment gcloud


【解决方案1】:

您使用的是相当低版本的 gunicorn 和 Flask。我会相应地升级它们 (google example) 并添加 firebase-admin、firebase 和其他一些依赖项。示例 requirements.txt:

Flask==1.1.1
gunicorn==20.0.4
moment==0.8.2
firebase==3.0.1
firebase-admin==3.2.1
google-api-python-client==1.7.11

我建议你将这些依赖项放在你的 requirements.txt 文件中,并且只在 virtualenv 中从该文件安装。

pip3 install -r requirements.txt

【讨论】:

    猜你喜欢
    • 2016-02-03
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 1970-01-01
    • 1970-01-01
    • 2018-02-14
    • 2018-05-05
    • 1970-01-01
    相关资源
    最近更新 更多