我在 Azure 中有一个 python Funktion
function_app.py
(每分钟触发一次),就像这样
import azure.functions as func
import logging
app = func.FunctionApp()
@app.timer_trigger(schedule="0 */1 * * * *", arg_name="myTimer", run_on_startup=False,
use_monitor=False)
def zsdbi(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('09 Python timer trigger function executed.')
在文件系统的同一级别上,我有一个文件
newconfig.py
,如下所示:
class MyConfig:
def __init__(self, ftp_host=None, ftp_username=None, ftp_password=None,
sharepoint_url=None, sharepoint_clientid=None, sharepoint_clientsecret=None, azure=False):
self._ftp_host = ftp_host
self._ftp_username = ftp_username
self._ftp_password = ftp_password
self._sharepoint_url = sharepoint_url
self._sharepoint_clientid = sharepoint_clientid
self._sharepoint_clientsecret = sharepoint_clientsecret
当我尝试像这样在
newconfig.py
中导入 function_app.py
时:
import azure.functions as func
import datetime
import json
import logging
import newconfig # This results in Error
app = func.FunctionApp()
@app.timer_trigger(schedule="0 */1 * * * *", arg_name="myTimer", run_on_startup=False,
use_monitor=False)
我认为该函数不再运行是由导入期间的错误引起的。如何将公共包中不可用的附加 python 文件添加到我的 Azure 函数中
将
newconfig.py
添加到与您的 function_app.py
相同的目录中。
如果
newconfig.py
位于子文件夹中,则需要相应调整导入路径。
示例: 如果
newconfig.py
放置在名为 config
的子文件夹中,您需要像这样导入它:
from config import newconfig
我创建了一个Python函数,其文件夹结构如下:
文件夹结构:
| .gitignore
| function_app.py
| host.json
| local.settings.json
| newconfig.py
| requirements.txt
|
+---.vscode
+---__blobstorage__
+---__pycache__
\---__queuestorage__
function_app.py:
import azure.functions as func
import datetime
import json
import logging
import newconfig
app = func.FunctionApp()
@app.timer_trigger(schedule="0 */5 * * * *", arg_name="myTimer", run_on_startup=False,
use_monitor=False)
def timer_trigger(myTimer: func.TimerRequest) -> None:
if myTimer.past_due:
logging.info('The timer is past due!')
logging.info('Python timer trigger function executed.')
控制台输出:
C:\Users\uname\pyfn>func start
Found Python version 3.11.9 (py).
Azure Functions Core Tools
Core Tools Version: 4.0.6280 Commit hash: N/A +421f0144b42047aa289ce691dc6db4fc8b6143e6 (64-bit)
Function Runtime Version: 4.834.3.22875
[2025-01-16T04:56:45.691Z] Worker process started and initialized.
Functions:
timer_trigger: timerTrigger
For detailed output, run func with --verbose flag.
[2025-01-16T04:56:50.481Z] Host lock lease acquired by instance ID '000000000000000000000000F72731CC'.
[2025-01-16T05:00:00.177Z] Executing 'Functions.timer_trigger' (Reason='Timer fired at 2025-01-16T10:30:00.0790914+05:30', Id=82facfd3-7236-4eab-a83f-339a45475c96)
[2025-01-16T05:00:00.337Z] Python timer trigger function executed.
[2025-01-16T05:00:00.389Z] Executed 'Functions.timer_trigger' (Succeeded, Id=82facfd3-7236-4eab-a83f-339a45475c96, Duration=280ms)