简体   繁体   English

FunctionApp 未导入 python 模块

[英]FunctionApp not importing python module

I've spent days trying to deploy a function app using Terraform.我花了几天时间尝试使用 Terraform 部署 function 应用程序。 It makes use of the requests python module.它利用了请求 python 模块。

I've narrowed the problem down to Azure not importing python modules in the requirements.txt.我已将问题缩小到 Azure 没有在 requirements.txt 中导入 python 模块。 file.文件。

To test, I created a simple httptrigger function app that simply returns a url parameter.为了测试,我创建了一个简单的 httptrigger function 应用程序,它只返回一个 url 参数。 My deployment script zips the directories and deploys the app using terraform.我的部署脚本压缩目录并使用 terraform 部署应用程序。 All is well and everything works as expected.一切都很好,一切都按预期工作。

But, if change my python script ( __init__.py ) to import the python modules “urllib3” and/or “requests,” The function fails, even though the simple python script doesn't actually use any methods or classes from the modules. But, if change my python script ( __init__.py ) to import the python modules “urllib3” and/or “requests,” The function fails, even though the simple python script doesn't actually use any methods or classes from the modules. There are no errors when deploying the function.部署 function 时没有错误。 It fails when called from the url.从 url 调用时失败。

I verified that the modules are listed in the requirements.txt file.我验证了这些模块在 requirements.txt 文件中列出。

And just to be sure, if I comment out the import requests and urllib3, everything works.可以肯定的是,如果我注释掉导入请求和 urllib3,一切正常。

Any advice or suggestions would be greatly appreciated.任何意见或建议将不胜感激。

Simple test script ( __init__.py ):简单的测试脚本( __init__.py ):

import logging
import azure.functions as func
# function fails with the following line.  Works if commented out.
import urllib3



def main(req: func.HttpRequest) -> func.HttpResponse:
    logging.info('Python HTTP trigger function processed a request.')

    target = req.params.get('target')
    if target:
        return func.HttpResponse("Pretending to use " + target, status_code = 503)
    else:
        return func.HttpResponse("No Taget Specified", status_code = 400)

requirements.txt (Updated 7/15) requirements.txt(7/15 更新)

azure-functions==1.2.1
future==0.18.2
pefile==2019.4.18
PyInstaller==3.6
pylint==2.5.3
pywin32-ctypes==0.2.0
PyYAML==5.3.1
urllib3==1.25.9
requests==2.24.1

Update 7/16: Terraform file: 7/16 更新:Terraform 文件:

variable "prefix" {
    type = "string"
    default = "jis"
}

variable "location" {
    type = "string"
    default = "eastus"
}

variable "environment" {
    type = "string"
    default = "dev"
}

variable "functionapp" {
    type = "string"
    default = "./testit.zip"
}

resource "random_string" "storage_name" {
    length = 24
    upper = false
    lower = true
    number = true
    special = false
}

provider "azurerm" {
  version = "~>2.1.0"
  features {}
}

# Create Storage Account

resource "azurerm_resource_group" "rg" {
    name = "${var.prefix}-${var.environment}"
    location = "${var.location}"
}

resource "azurerm_storage_account" "storage" {
    name = "${random_string.storage_name.result}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    location = "${var.location}"
    account_tier = "Standard"
    account_replication_type = "LRS"
}

resource "azurerm_storage_container" "deployments" {
    name = "function-releases"
    storage_account_name = "${azurerm_storage_account.storage.name}"
    container_access_type = "private"
}

# upload zip file

resource "azurerm_storage_blob" "appcode" {
    name = "functionapp.zip"
    storage_account_name = "${azurerm_storage_account.storage.name}"
    storage_container_name = "${azurerm_storage_container.deployments.name}"
    type = "Block"
    source = "${var.functionapp}"
}

# Create function app

data "azurerm_storage_account_sas" "sas" {
    connection_string = "${azurerm_storage_account.storage.primary_connection_string}"
    https_only = true
    start = "2020-07-10"
    expiry = "2023-12-31"
    resource_types {
        object = true
        container = false
        service = false
    }
    services {
        blob = true
        queue = false
        table = false
        file = false
    }
    permissions {
        read = true
        write = false
        delete = false
        list = false
        add = false
        create = false
        update = false
        process = false
    }
}

resource "azurerm_app_service_plan" "asp" {
    name = "${var.prefix}-plan"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    location = "${var.location}"
    kind = "Linux"
    reserved = true
    sku {
        tier = "Dynamic"
        size = "Y1"
    }
}

resource "azurerm_function_app" "functions" {
    name = "${var.prefix}-${var.environment}"
    location = "${var.location}"
    resource_group_name = "${azurerm_resource_group.rg.name}"
    app_service_plan_id = "${azurerm_app_service_plan.asp.id}"
    storage_connection_string = "${azurerm_storage_account.storage.primary_connection_string}"
    version = "~2"

    app_settings = {
        https_only = true
        FUNCTIONS_WORKER_RUNTIME = "python"
        WEBSITE_NODE_DEFAULT_VERSION = "~10"
        FUNCTION_APP_EDIT_MODE = "readonly"
        HASH = "${base64encode(filesha256("${var.functionapp}"))}"
        WEBSITE_RUN_FROM_PACKAGE = "https://${azurerm_storage_account.storage.name}.blob.core.windows.net/${azurerm_storage_container.deployments.name}/${azurerm_storage_blob.appcode.name}${data.azurerm_storage_account_sas.sas.sas}"
    }
}

# 

It may be a version compatibility problem.可能是版本兼容性问题。

The versions of urllib3 and requests that you are using are from June 2018, the others (did not check all) appear to be latest versions.您使用的 urllib3 和 requests 的版本来自 2018 年 6 月,其他(未检查全部)似乎是最新版本。

Any reason why you need to use the older versions?为什么需要使用旧版本?

After some struggling, I figured out the problem.经过一番挣扎,我发现了问题所在。 Thanks to @HuryShen and @AnkitKumar for pointing me in the right direction.感谢@HuryShen 和@AnkitKumar 为我指明了正确的方向。

I did not include the modules urllib3 and requests in the function zip file.我没有在 function zip 文件中包含模块 urllib3 和 requests。 They need to be installed to the <funcname>.python_packages/lib/site-packages directory .它们需要安装到<funcname>.python_packages/lib/site-packages directory The easiest way to do that (from the requirements.txt file) is最简单的方法(来自 requirements.txt 文件)是

pip install  --target="azure/.python_packages/lib/site-packages"  -r requirements.txt

Once the modules are downloaded, they can be included in the zip file.下载模块后,它们可以包含在 zip 文件中。 After that, the terraform script works fine.之后,terraform 脚本工作正常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM