問題描述
透過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.txtWhen 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-buildRemember to replace <APP_NAME> with the name of your function app in Azure.
【完】