如何解决Azure App Service WebJobs中运行Python任务的报错问题
问题
在Azure App Service的Linux环境(Basic计划、Python运行时、Code部署模型)中,上传包含Python脚本的ZIP包创建WebJob后运行报错,错误日志如下:
[11/18/2024 23:23:22 > d7204e: SYS INFO] Status changed to Initializing [11/18/2024 23:23:22 > d7204e: SYS INFO] Run script 'run.py' with script host - 'PythonScriptHost' [11/18/2024 23:23:22 > d7204e: SYS INFO] Status changed to Running [11/18/2024 23:23:22 > d7204e: ERR ] An error occurred trying to start process 'python.exe' with working directory '/tmp/jobs/triggered/webjobpy/bphw5fzr.pno/webjobtest'. No such file or directory
推测问题是Linux环境下找不到python.exe,需解决该报错并确认WebJobs是否支持Python任务。
解决方案
- 指定Linux兼容的Python可执行文件:Linux系统中没有
python.exe,需使用python3或python(取决于App Service配置的Python版本),可通过两种方式配置:- 给WebJob添加启动脚本:在ZIP包中创建
run.sh文件,内容为:
本地执行#!/bin/bash python3 run.pychmod +x run.sh赋予脚本可执行权限后再打包上传,WebJob会优先执行该脚本。 - 修改WebJob运行命令:创建WebJob时,将默认的脚本执行命令改为
python3 run.py,而非直接指定run.py。
- 给WebJob添加启动脚本:在ZIP包中创建
- 验证Python运行时可用性:通过App Service的SSH连接进入容器,执行
which python3确认可执行文件路径是否存在,同时检查App Service配置中是否正确启用了目标版本的Python运行时。 - 明确WebJobs对Python的支持性:Azure WebJobs完全支持Python任务,本次报错仅因默认脚本主机误用了Windows平台的
python.exe命令,适配Linux环境的可执行文件名称即可解决。
内容的提问来源于stack exchange,提问作者okake
相关产品推荐
相关产品推荐

