运行 Python Azure 应用服务上的 2 个应用程序时出现包导入错误

Package import error when running Python 2 app on Azure App Services

该应用程序在我的本地 linux 服务器上运行良好,但我在 Azure 上运行 遇到问题。

我检查了 Files 文件夹,所有 python 库都正确安装在 /antenv2.7/lib/python2.7/site-packages.

这是我的 pip 要求文件:

bs4==0.0.1
Flask==1.0.2
Flask-Cors==3.0.7
Keras==2.2.0
nltk==3.2.5
numpy==1.14.5
pandas==0.23.3
scikit-learn==0.19.1
scipy==1.1.0
tensorflow==1.9.0
protobuf==3.9.1

但是当gunicorn启动时,会产生这个错误。我不知道为什么无法导入 protobuf,而 Keras 等其他库可以毫无问题地导入。

这是完整的堆栈跟踪。谢谢!

2019-08-29T21:09:37.697162031Z 
2019-08-29T21:09:37.697200631Z   _____                               
2019-08-29T21:09:37.697207831Z   /  _  \ __________ _________   ____  
2019-08-29T21:09:37.697212431Z  /  /_\  \___   /  |  \_  __ \_/ __ \ 
2019-08-29T21:09:37.697216931Z /    |    \/    /|  |  /|  | \/\  ___/ 
2019-08-29T21:09:37.697221431Z \____|__  /_____ \____/ |__|    \___  >
2019-08-29T21:09:37.697226031Z         \/      \/                  \/ 
2019-08-29T21:09:37.697230631Z 
2019-08-29T21:09:37.697234631Z A P P   S E R V I C E   O N   L I N U X
2019-08-29T21:09:37.697238731Z 
2019-08-29T21:09:37.697242831Z Documentation: http://aka.ms/webapp-linux
2019-08-29T21:09:37.697247031Z 
2019-08-29T21:09:38.233956948Z Starting OpenBSD Secure Shell server: sshd.
2019-08-29T21:09:38.323527534Z Site's appCommandLine: startup.txt
2019-08-29T21:09:38.329445366Z Checking of startup.txt is a file
2019-08-29T21:09:38.331698079Z App command line is a file on disk
2019-08-29T21:09:38.342990940Z startup.txt file exists on disk, reading its contents to run as startup arguments
2019-08-29T21:09:38.343698744Z Contents of startupScript: gunicorn --bind=0.0.0.0 --timeout 600 server:app
2019-08-29T21:09:38.344176546Z Launching oryx with: -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv2.7 -defaultApp /opt/defaultsite -bindPort 8000 -userStartupCommand 'gunicorn --bind=0.0.0.0 --timeout 600 server:app'
2019-08-29T21:09:38.469155825Z Oryx Version : 0.2.20190518.2, Commit: 5e1ddd1855bcb53ce686e2124ed6e9603cb0587a
2019-08-29T21:09:38.469591228Z 
2019-08-29T21:09:39.415029965Z Writing output script to '/opt/startup/startup.sh'
2019-08-29T21:09:39.467672451Z Using 'oryx-manifest.toml'.
2019-08-29T21:09:39.472390477Z Checking if virtual environment was compressed...
2019-08-29T21:09:39.729331673Z Using packages from virtual environment 'antenv2.7' located at '/home/site/wwwroot/antenv2.7'.
2019-08-29T21:09:43.204792157Z [2019-08-29 21:09:43 +0000] [35] [INFO] Starting gunicorn 19.9.0
2019-08-29T21:09:43.206124364Z [2019-08-29 21:09:43 +0000] [35] [INFO] Listening at: http://0.0.0.0:8000 (35)
2019-08-29T21:09:43.206716767Z [2019-08-29 21:09:43 +0000] [35] [INFO] Using worker: sync
2019-08-29T21:09:43.224584364Z [2019-08-29 21:09:43 +0000] [39] [INFO] Booting worker with pid: 39
2019-08-29T21:10:08.320887834Z Using TensorFlow backend.
2019-08-29T21:10:09.701427737Z [2019-08-29 21:10:09 +0000] [39] [ERROR] Exception in worker process
2019-08-29T21:10:09.701474537Z Traceback (most recent call last):
2019-08-29T21:10:09.701479937Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/arbiter.py", line 583, in spawn_worker
2019-08-29T21:10:09.701484237Z     worker.init_process()
2019-08-29T21:10:09.701487937Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 129, in init_process
2019-08-29T21:10:09.701498537Z     self.load_wsgi()
2019-08-29T21:10:09.701502237Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/workers/base.py", line 138, in load_wsgi
2019-08-29T21:10:09.701505937Z     self.wsgi = self.app.wsgi()
2019-08-29T21:10:09.701509337Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/base.py", line 67, in wsgi
2019-08-29T21:10:09.701512937Z     self.callable = self.load()
2019-08-29T21:10:09.701516437Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 52, in load
2019-08-29T21:10:09.701520037Z     return self.load_wsgiapp()
2019-08-29T21:10:09.701523337Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py", line 41, in load_wsgiapp
2019-08-29T21:10:09.701527037Z     return util.import_app(self.app_uri)
2019-08-29T21:10:09.701530537Z   File "/usr/local/lib/python2.7/site-packages/gunicorn/util.py", line 350, in import_app
2019-08-29T21:10:09.701579737Z     __import__(module)
2019-08-29T21:10:09.701612538Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/keras/__init__.py", line 3, in <module>
2019-08-29T21:10:09.701616338Z     from . import utils
2019-08-29T21:10:09.701619738Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/keras/utils/__init__.py", line 6, in <module>
2019-08-29T21:10:09.701623538Z     from . import conv_utils
2019-08-29T21:10:09.701626838Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/keras/utils/conv_utils.py", line 9, in <module>
2019-08-29T21:10:09.701630638Z     from .. import backend as K
2019-08-29T21:10:09.701633938Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/keras/backend/__init__.py", line 87, in <module>
2019-08-29T21:10:09.701637638Z     from .tensorflow_backend import *
2019-08-29T21:10:09.701647638Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/keras/backend/tensorflow_backend.py", line 5, in <module>
2019-08-29T21:10:09.701652338Z     import tensorflow as tf
2019-08-29T21:10:09.701659738Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/tensorflow/__init__.py", line 22, in <module>
2019-08-29T21:10:09.701663538Z     from tensorflow.python import pywrap_tensorflow  # pylint: disable=unused-import
2019-08-29T21:10:09.701667138Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/tensorflow/python/__init__.py", line 52, in <module>
2019-08-29T21:10:09.701670938Z     from tensorflow.core.framework.graph_pb2 import *
2019-08-29T21:10:09.701674438Z   File "/home/site/wwwroot/antenv2.7/lib/python2.7/site-packages/tensorflow/core/framework/graph_pb2.py", line 6, in <module>
2019-08-29T21:10:09.701678038Z     from google.protobuf import descriptor as _descriptor
2019-08-29T21:10:09.701681538Z ImportError: No module named google.protobuf
2019-08-29T21:10:09.714154906Z [2019-08-29 21:10:09 +0000] [39] [INFO] Worker exiting (pid: 39)
2019-08-29T21:10:10.020760372Z [2019-08-29 21:10:10 +0000] [35] [INFO] Shutting down: Master
2019-08-29T21:10:10.022103379Z [2019-08-29 21:10:10 +0000] [35] [INFO] Reason: Worker failed to boot.

部署完成后,我必须在 site-packages/google/ 下手动添加 __init__.py

Azure 应用服务运行 Debian linux。也许 python 在这种 linux 上运行有点不同。然而,我的应用程序在我的 Ubuntu 虚拟机上正常运行,无需执行此操作。

Azure 应用服务是垃圾。差不多。