我在使用 zip 部署将 Python Flask 应用程序部署到 Azure 应用服务(Python 3.9) 时遇到问题。详情如下:
工作代码:
app.py:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Azure!--1"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
需求.txt:
Flask==3.1.0
非工作代码:
app.py:
from flask import Flask
import openai
app = Flask(__name__)
@app.route('/')
def hello():
return "Hello, Azure!--1"
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000)
需求.txt:
Flask==3.1.0
openai==1.57.1
API 日志流中出现错误:
Site's appCommandLine: gunicorn --bind=0.0.0.0:8000 app:app
Launching oryx with: create-script -appPath /home/site/wwwroot -output /opt/startup/startup.sh -virtualEnvName antenv -defaultApp /opt/defaultsite -userStartupCommand 'gunicorn --bind=0.0.0.0:8000 app:app'
Could not find build manifest file at '/home/site/wwwroot/oryx-manifest.toml'
Could not find operation ID in manifest. Generating an operation id...
Build Operation ID: c9b33d68-8d6d-4326-9b08-257fe923d135
Oryx Version: 0.2.20240619.2, Commit: cf006407a02b225f59dccd677986973c7889aa50, ReleaseTagName: 20240619.2
Writing output script to '/opt/startup/startup.sh'
WARNING: Could not find virtual environment directory /home/site/wwwroot/antenv.
WARNING: Could not find package directory /home/site/wwwroot/__oryx_packages__.
Booting worker with pid: 1075
[1075] [ERROR] Exception in worker process
Traceback (most recent call last):
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/arbiter.py", line 609, in spawn_worker
worker.init_process()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/workers/base.py", line 134, in init_process
self.load_wsgi()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/workers/base.py", line 146, in load_wsgi
self.wsgi = self.app.wsgi()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/app/base.py", line 67, in wsgi
self.callable = self.load()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 58, in load
return self.load_wsgiapp()
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/app/wsgiapp.py", line 48, in load_wsgiapp
return util.import_app(self.app_uri)
File "/opt/python/3.9.19/lib/python3.9/site-packages/gunicorn/util.py", line 371, in import_app
mod = importlib.import_module(module)
File "/opt/python/3.9.19/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1030, in _gcd_import
File "<frozen importlib._bootstrap>", line 1007, in _find_and_load
File "<frozen importlib._bootstrap>", line 986, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 680, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 850, in exec_module
File "<frozen importlib._bootstrap>", line 228, in _call_with_frames_removed
File "/home/site/wwwroot/app.py", line 2, in <module>
import openai
ModuleNotFoundError: No module named 'openai'
[1075] [INFO] Worker exiting (pid: 1075)
[1064] [ERROR] Worker (pid:1075) exited with code 3
[1064] [ERROR] Shutting down: Master
[1064] [ERROR] Reason: Worker failed to boot.
我已验证该代码在本地可以正常运行,但在部署到 Azure 应用服务时会失败。任何有关可能导致此问题的原因的见解或建议将不胜感激!
提前致谢!
我参考了可用于创建和部署示例 Flask 应用程序的文档,并在 VS Code 中将其克隆到本地。
我还长期使用了您提供的相同代码以及requirements.txt 文件中的Flask 和openai 模块。
然后,我创建了虚拟环境并使用以下命令激活它。
py -m venv .venv
.venv\scripts\activate
使用
pip install -r requirements.txt
命令安装依赖项。
它对我来说在当地很有效,就像你一样。
我创建了一个 Linux Azure 应用程序服务,运行时为 python 3.9。
然后,我使用 vs code 部署了代码。
我能够成功访问应用程序。
您可能会因部署不当而遇到此问题。正如其显示的 python 无法找到虚拟环境目录 /home/site/wwwroot/antenv 警告,这意味着虚拟环境未创建或找不到它。因此,您可以使用任何其他替代方式(例如 vs code 或 cli 命令)进行部署。