我正在开发将部署到 GCP App Engine 的 Python 应用程序。
我尝试使用python-magic
功能。
我已添加到import magic
我的代码和文件中。
但是,当我尝试将代码部署到 App Engine 时,它会失败并出现以下错误:python-magic
requirements.txt
[2020-02-03 15:35:23 +0000] [8] [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 11, in <module>
import magic
File "/env/lib/python3.6/site-packages/magic.py", line 181, in <module>
raise ImportError('failed to find libmagic. Check your installation')
ImportError: failed to find libmagic. Check your installation
[2020-02-03 15:35:23 +0000] [8] [INFO] Worker exiting (pid: 8)
[2020-02-03 15:35:23 +0000] [1] [INFO] Shutting down: Master
[2020-02-03 15:35:23 +0000] [1] [INFO] Reason: Worker failed to boot.
我缺少什么来完成这项工作?
编辑:共享最小设置以重现此错误
主文件
# -*- coding: utf-8 -*-
import json
import base64
import magic
import logging
logging.basicConfig(level=logging.DEBUG)
from flask import Flask, request, abort
app = Flask(__name__)
@app.route('/parse', methods=['POST'])
def parse():
try:
#input should be a b64 encoded byte string
input = request.get_json()['input']
except Exception as e:
abort(400, 'An error occured while reading in parameters: {}'.format(str(e)))
decoded_input_data = base64.decodebytes(input)
mime = magic.Magic(mime=True)
file_ext = mime.from_buffer(decoded_input_data)
returnData = {}
returnData['Status'] = 200
returnData['file_ext'] = file_ext
return json.dumps(returnData), 200
@app.errorhandler(Exception)
def error(e):
logging.exception(str(e))
return json.dumps({'Status': e.code,
'Message': e.description}), e.code
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='127.0.0.1', port=8080, debug=True)
要求.txt
Flask==1.0.2
gunicorn==19.9.0
google-cloud-vision==0.38.0
google-cloud-bigquery==1.11.2
google-cloud-storage==1.15.0
requests-toolbelt==0.9.1
protobuf==3.6.0
python-magic==0.4.15
应用程序.yaml
runtime: python
env: flex
service: myservicename
entrypoint: gunicorn -b :$PORT main:app --timeout 240 --limit-request-line 0
runtime_config:
python_version: 3