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

本文涉及的产品
性能测试 PTS,5000VUM额度
注册配置 MSE Nacos/ZooKeeper,118元/月
任务调度 XXL-JOB 版免费试用,400 元额度,开发版规格
简介: 通过FTP上传Python Function至Azure云后,出现函数列表无法加载的问题。经排查,发现是由于`requirements.txt`中的依赖包未被正确安装。解决方法为:在本地安装依赖包到`.python_packages/lib/site-packages`目录,再将该目录内容上传至云上的`wwwroot`目录,并重启应用。最终成功加载函数列表。

问题描述

通过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,成功加载出函数列表!

 

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

 

参考资料

  1. 部署后找不到函数 : https://docs.azure.cn/zh-cn/azure-functions/recover-python-functions?tabs=vscode%2Cbash&pivots=python-mode-configuration#functions-not-found-after-deployment
  2. 使用 FTP/S 将应用部署到 Azure 应用服务 : https://docs.azure.cn/zh-cn/app-service/deploy-ftp?tabs=portal
  3. 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.

 

 

【完】



 

当在复杂的环境中面临问题,格物之道需:浊而静之徐清,安以动之徐生。 云中,恰是如此!

相关实践学习
日志服务之使用Nginx模式采集日志
本文介绍如何通过日志服务控制台创建Nginx模式的Logtail配置快速采集Nginx日志并进行多维度分析。
相关文章
|
17天前
|
C#
【Azure Function】Function App出现System.IO.FileNotFoundException异常
Exception while executing function: xxxxxxx,The type initializer for 'xxxxxx.Storage.Adls2.StoreDataLakeGen2Reading' threw an exception. Could not load file or assembly 'Microsoft.Extensions.Configuration, Version=9.0.0.0, Culture=neutral, PublicKeyToken=adb9793829ddae60'. The system cannot find the
113 64
|
1月前
|
JSON C# 数据格式
【Azure Function】C#独立工作模式下参数类型 ServiceBusReceivedMessage 无法正常工作
Cannot convert input parameter 'message' to type 'Azure.Messaging.ServiceBus.ServiceBusReceivedMessage' from type 'System.String'.
111 73
|
3天前
|
JavaScript API
【Azure Function】Function App门户上的Test/Run返回错误:Failed to fetch
Running your function in portal requires the app to explicitly accept requests from https://portal.azure.cn. This is known as cross-origin resource sharing (CORS).Configure CORS to add https://portal.azure.cn to allowed origins.
|
1月前
|
Java Windows
【Azure Function】部署Java Function失败:报错deploy [ERROR] Status code 401和警告 'China North 3' may not be a valid region
1:deploy [ERROR] Status code 401, (empty body). 2: China North 3 may not be a valid region,please refer to https://aka.ms/maven_function_configuration#supported-regions for values. 3:  <azure.functions.maven.plugin.version>1.36.0</azure.functions.maven.plugin.version>
38 11
|
2月前
|
JavaScript API C#
【Azure Developer】Python代码调用Graph API将外部用户添加到组,结果无效,也无错误信息
根据Graph API文档,在单个请求中将多个成员添加到组时,Python代码示例中的`members@odata.bind`被错误写为`members@odata_bind`,导致用户未成功添加。
50 10
|
2月前
|
API Python
【Azure Developer】分享一段Python代码调用Graph API创建用户的示例
分享一段Python代码调用Graph API创建用户的示例
68 11
|
4月前
|
机器人 Shell Linux
【Azure Bot Service】部署Python ChatBot代码到App Service中
本文介绍了使用Python编写的ChatBot在部署到Azure App Service时遇到的问题及解决方案。主要问题是应用启动失败,错误信息为“Failed to find attribute &#39;app&#39; in &#39;app&#39;”。解决步骤包括:1) 修改`app.py`文件,添加`init_func`函数;2) 配置`config.py`,添加与Azure Bot Service认证相关的配置项;3) 设置App Service的启动命令为`python3 -m aiohttp.web -H 0.0.0.0 -P 8000 app:init_func`。
|
4月前
|
Linux Python
【Azure Function】Python Function部署到Azure后报错No module named '_cffi_backend'
ERROR: Error: No module named '_cffi_backend', Cannot find module. Please check the requirements.txt file for the missing module.
110 2
|
2月前
|
人工智能 数据可视化 数据挖掘
探索Python编程:从基础到高级
在这篇文章中,我们将一起深入探索Python编程的世界。无论你是初学者还是有经验的程序员,都可以从中获得新的知识和技能。我们将从Python的基础语法开始,然后逐步过渡到更复杂的主题,如面向对象编程、异常处理和模块使用。最后,我们将通过一些实际的代码示例,来展示如何应用这些知识解决实际问题。让我们一起开启Python编程的旅程吧!