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

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

我所做的是将函数文件夹作为标准路径插入,以便 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/

相关文章:

python - 将 virtualenv python 3 升级到 3.4.3

python - 将多个项目插入 PostgreSQL 表

python - 如何正确解析带内引号的 CSV 文件?

c# - Azure DevOps 评论中的 Ping (@) 用户

c# - Azure 搜索,精确短语匹配

python - 全局和本地 python 安装,意外地在 virtualenv 之外运行需求文件

python - pymunk + tkinter : How to get ball to move to left or right with pymunk?

python - psycopg2.errors.InFailedSqlTransaction : current transaction is aborted, 命令被忽略,直到事务 block 结束

python - Python3 的 sqlite3 模块中自动提交的问题

java - 无法在 Mac 中为 Azure SQL 数据库打开端口 1433