在Azure python函数中导入自定义模块

时间:2019-10-10 08:41:16

标签: python python-3.x azure azure-functions

我正在构建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}")]

我所做的是将我的function文件夹作为标准路径插入,因此python也在该文件夹中查找库。使用Visual Studio Code,此功能在本地环境中可完美运行。但是,当我部署并运行它时,它会抛出myCustomModule未找到。另一条信息是我无法访问Kudu(终端),因为我的Linux消费计划未提供该支持。我不确定我想念的是什么。请不要,因为它不是标准库,所以我不能将其添加到requirements.txt中。另外请注意,我的function文件夹包含我的功能脚本文件和自定义模块,因此它们与azure python模板中的文件夹位于同一文件夹中。

1 个答案:

答案 0 :(得分:1)

使用绝对路径而不是相对路径。

以下为我工作

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}")]