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

2024-09-29 21:43:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在构建一个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也会在该文件夹中查找库。此函数在使用visualstudio代码的本地环境中可以完美地工作。但是,当我部署并运行它时,它抛出了找不到的myCustomModule。另一条信息是我无法访问Kudu(终端),因为我的Linux消费计划中不支持它。我不知道我错过了什么。请不要,因为它不是一个标准库,我不能把它添加到要求.txt. 另外请注意,我的函数文件夹有我的函数脚本文件和我的自定义模块,因此它们与azurepython模板中应该位于同一个文件夹中。在


Tags: 模块path函数代码fromimport文件夹as
1条回答
网友
1楼 · 发布于 2024-09-29 21:43:25

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

跟随对我有用

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

相关问题 更多 >

    热门问题