在Azure python函数中导入自定义模块 [英] Import custom modules in azure python function

查看:58
本文介绍了在Azure python函数中导入自定义模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建python函数,我需要在代码中添加我的自定义模块.我的azure函数如下所示:

I am building a python function I and I need to add my custom module in the code. My azure function looks like this:

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模板中的文件夹位于同一文件夹中.

What I have done is inserted my function folder as a standard path so python looks for the libraries in that folder as well. This function works perfectly in the local environment using Visual Studio Code. However, when I deploy and run it, it throws myCustomModule not found. Another piece of information is that I cannot access Kudu (terminal) since it is not suppored on my consumption plan for Linux. I am not sure what am I missing. Please not since its not a standard library I cannot add it in the requirements.txt. Also note, that my function folder has my function script file and my custom module so they are in the same folder as it should be in the azure python template.

推荐答案

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

Use an absolute path rather than the relative paths.

以下为我工作

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

这篇关于在Azure python函数中导入自定义模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆