我正在构建一个 python 函数,我需要在代码中添加我的自定义模块。我的 azure 函数如下所示:
import logging
import sys
from sys import path
import os
import time
sys.path.insert(0, './myFunctionFolderName') // Try to insert it as a standard python library.
import azure.functions as func
from myCustomModule.models import someFunctionName
def main(req: func.HttpRequest) -> func.HttpResponse:
name = "My new function"
testing_variable = someFunctionName()
return func.HttpResponse(f"Function executed {name}")]
我所做的是将函数文件夹作为标准路径插入,以便 python 也在该文件夹中查找库。该功能在使用 Visual Studio Code 的本地环境中完美运行。但是,当我部署并运行它时,它会抛出找不到 myCustomModule 的错误。另一条信息是我无法访问 Kudu(终端),因为我的 Linux 消费计划不支持它。我不确定我错过了什么。请不要,因为它不是标准库,我无法将其添加到requirements.txt中。另请注意,我的函数文件夹包含函数脚本文件和自定义模块,因此它们与 azure python 模板应位于同一文件夹中。
最佳答案
使用绝对路径而不是相对路径。
以下内容对我有用
import logging
import sys
from sys import path
import os
import time
dir_path = os.path.dirname(os.path.realpath(__file__))
sys.path.insert(0, dir_path)
import azure.functions as func
from myCustomModule.models import someFunctionName
def main(req: func.HttpRequest) -> func.HttpResponse:
name = "My new function"
testing_variable = someFunctionName()
return func.HttpResponse(f"Function executed {name}")]
关于python - 在azure python函数中导入自定义模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58318794/