【Azure Function】FTP上传了Python Function文件后,无法在门户页面加载函数的问题

问题描述

通过FTP的方式,把本地能正常运行的Python Function文件上传到云上后,无法加载函数列表问题。

1:上传 function_app.py,requirements.txt文件到 wwwroot 目录中

2:在Azure Function App的Overview页面,无法显示函数列表

3:查看所有日志,无任何异常信息,Docker 日志中显示Host启动成功,function host的日志中,没有错误显示,但记录 0 functions load

### LogFiles/2024_11_21_pl0sdlwk000620_docker.log 日志显示:

2024-11-21T12:28:35.622Z INFO - Pulling image: mcr.microsoft.com/appsvc/middleware:stage6
2024-11-21T12:28:36.520Z INFO - stage6 Pulling from appsvc/middleware
2024-11-21T12:28:36.533Z INFO - Digest: sha256:
2024-11-21T12:28:36.536Z INFO - Status: Image is up to date for mcr.microsoft.com/appsvc/middleware:stage6
2024-11-21T12:28:36.569Z INFO - Pull Image successful, Time taken: 0 Seconds
2024-11-21T12:28:36.696Z INFO - Starting container for site
2024-11-21T12:28:36.697Z INFO - docker run -d --expose=8181 --name lbfunbyftp01_5_6c1bcea1_middleware -e WEBSITE_CORS_ALLOWED_ORIGINS=https://portal.azure.cn -e ....... Host.UseFileLogging=true
2024-11-21T12:28:36.697Z INFO - Logging is not enabled for this container.
Please use https://aka.ms/lin2024-11-21T12:28:37.758Z INFO - Initiating warmup request to container lbfunbyftp01_5_6c1bcea1 for site lbfunbyftp01
2024-11-21T12:28:43.089Z INFO - Container lbfunbyftp01_5_6c1bcea1 for site lbfunbyftp01 initialized successfully and is ready to serve requests.
2024-11-21T12:28:43.089Z INFO - Initiating warmup request to container lbfunbyftp01_5_6c1bcea1_middleware for site lbfunbyftp01
2024-11-21T12:28:43.415Z INFO - Container lbfunbyftp01_5_6c1bcea1_middleware for site lbfunbyftp01 initialized successfully and is ready to serve requests.

 

### ../LogFiles/Application/Functions/Host/2024-11-20T12-56-11Z-1ffcb4d1cf.log 日志显示:

2024-11-21T12:29:12.670 [Information] Loading functions metadata
2024-11-21T12:29:12.670 [Information] Reading functions metadata (Custom)
2024-11-21T12:29:12.671 [Information] 0 functions found (Custom)
2024-11-21T12:29:12.671 [Information] 0 functions loaded

 

这是一个什么情况呢?

 

问题解答

在遇见此问题后,百思不得其解,最后采用了最笨的办法。逐行/逐段的删除代码,一次一次的查找到底是什么代码导致了这个问题。

先使用一个近似于模板的Python Http Trigger 代码,没有任何多余引用的情况下,函数加载成功!

基于此次发现,一行一行的添加代码,终于,在本次实验中,添加到  import requests 时候,复现问题。 

瞬间,明白了原因,函数无法被加载在Overview显示的原因是缺少了 requests module,虽然在 requirements.txt 中添加了requests module,但是Function App并没有帮助安装。

所以,解决问题之法就是本地上传所需要的相关依赖包!

 

解决办法

第一步:在本地安装 requirements.txt 中的依赖包到.python_packages文件夹中

使用下面的命令,把依赖包安装到 ".python_packages/lib/site-packages" 文件夹中

python -m pip install -r .\requirements.txt  --target=".python_packages/lib/site-packages"   

第二步:把 .python_packages 文件夹中的内容通过FTP上传 Function App的wwwroot目录中

第三步:重启应用,等待5-10分钟后,刷新Function Overview,成功加载出函数列表!

 

如此,问题解决!完美收工。

 

参考资料

部署后找不到函数 : https://docs.azure.cn/zh-cn/azure-functions/recover-python-functions?tabs=vscode%2Cbash&pivots=python-mode-configuration#functions-not-found-after-deployment

使用 FTP/S 将应用部署到 Azure 应用服务 : https://docs.azure.cn/zh-cn/app-service/deploy-ftp?tabs=portal

Install local packages : https://learn.microsoft.com/en-us/azure/azure-functions/functions-reference-python?tabs=get-started%2Casgi%2Capplication-level&pivots=python-mode-decorators#install-local-packages

If your project uses packages that aren't publicly available to our tools, you can make them available to your app by putting them in the __app__/.python_packages directory. Before you publish, run the following command to install the dependencies locally:

pip install --target="<PROJECT_DIR>/.python_packages/lib/site-packages" -r requirements.txt

When you're using custom dependencies, you should use the --no-build publishing option, because you've already installed the dependencies into the project folder.

 func azure functionapp publish <APP_NAME> --no-build

Remember to replace <APP_NAME> with the name of your function app in Azure.

 

 

【完】

 

posted @ 2024-11-21 21:10  路边两盏灯  阅读(80)  评论(0编辑  收藏  举报