Python 脚本中的 Azure 函数 ModuleNotFoundError [英] Azure Function ModuleNotFoundError in Python script

查看:19
本文介绍了Python 脚本中的 Azure 函数 ModuleNotFoundError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在 Azure Function 中运行以下脚本:初始化.py

I have the following script which I am trying to run in Azure Function: init.py

import logging
import azure.functions as func
import pandas as pd
import numpy as np
from datetime import datetime

def main(myblob: func.InputStream):
    logging.info(f"Python blob trigger function processed blob 
"
                 f"Name: {myblob.name}
"
                 f"Blob Size: {myblob.length} bytes")

这里是function.json:

Here is the function.json:

{
    "scriptFile": "__init__.py",
    "bindings": [
        {
            "name": "myblob",
            "type": "blobTrigger",
            "direction": "in",
            "path": "uwci-sftp-rb92351a6c-41fa-4b90-aa79-4e9974ca83f7/{name}",
            "connection": ""
        }
    ]
}

当我只导入 azure.functions 和日志时,它工作正常.我仅在尝试在 Azure Function 中运行代码时收到此错误.对于 pandas 或任何其他库,我收到以下错误:

It was working fine when I imported only azure.functions and logging. I am getting this error only while trying to run the code in Azure Function. I am getting the following error for pandas or any other library for that matter:

Result: Failure
Exception: ModuleNotFoundError: No module named 'pandas'
Stack:   File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/dispatcher.py", line 242, in _handle__function_load_request
    func_request.metadata.entry_point)
  File "/azure-functions-host/workers/python/3.7/LINUX/X64/azure_functions_worker/loader.py", line 66, in load_function
    mod = importlib.import_module(fullmodname)
  File "/usr/local/lib/python3.7/importlib/__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/home/site/wwwroot/Test-UWCI2/__init__.py", line 3, in <module>
    import pandas as pd ```

推荐答案

您需要在代码中包含一个 requirements.txt 文件,其中列出了您的函数的所有 python 依赖项.

You need to include a requirements.txt file with your code which lists all the python dependencies of your function.

requirements.txt: 包含发布到 Azure 时系统安装的软件包列表.

requirements.txt: Contains the list of packages the system installs when publishing to Azure.

参考:

https://docs.microsoft.com/en-us/azure/azure-functions/functions-reference-python#python-version-and-package-management

这篇关于Python 脚本中的 Azure 函数 ModuleNotFoundError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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