pandas Azure函数应用程序错误:属性错误:模块“os”没有属性“add_dll_directory”,

cvxl0en2  于 12个月前  发布在  其他
关注(0)|答案(1)|浏览(104)

我正在使用pyhton(3.10.9)构建Azure Function应用程序,并在部署后从应用程序中获得以下错误。我怀疑这个错误是由于函数app的操作系统,它是Linux,不支持pandas中numpy使用的“add_dll_directory”方法。当我在Visual Studio中调试应用程序时,它工作正常。任何建议,我可以如何解决这个问题?
代码:

import azure.functions as func

app = func.FunctionApp()

@app.function_name(name="myTimer")
@app.schedule(schedule="*/15 * * * *", arg_name="myTimer", run_on_startup=True, use_monitor=False)

def main(myTimer: func.TimerRequest) -> None:
    from batch_functions import price_data_download, position_data_download, trade_history_download, risk_checks
    import logging

    price_data_download()
    position_data_download()
    risk_checks()
    trade_history_download()
    if myTimer.past_due:
        logging.info('The timer is past due!')

    logging.info('Python timer trigger function executed.')
Result: Failure Exception: AttributeError: module 'os' has no attribute 'add_dll_directory' Stack: File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 479, 
in _handle__invocation_request call_result = await self._loop.run_in_executor( File "/usr/local/lib/python3.10/concurrent/futures/thread.py", 
line 58, 
in run result = self.fn(*self.args, **self.kwargs) File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/dispatcher.py", line 752, 
in _run_sync_func return ExtensionManager.get_sync_invocation_wrapper(context, File "/azure-functions-host/workers/python/3.10/LINUX/X64/azure_functions_worker/extension.py", line 215, 
in _raw_invocation_wrapper result = function(**args) File "/home/site/wwwroot/function_app.py", line 9, 
in main from batch_functions import price_data_download, position_data_download, trade_history_download, risk_checks File "/home/site/wwwroot/batch_functions.py", line 1, 
in <module> import pandas as pd File "/home/site/wwwroot/pandas/__init__.py", line 24, 
in <module> __import__(_dependency) File "/home/site/wwwroot/numpy/__init__.py", line 112, 
in <module> _delvewheel_patch_1_5_1() File "/home/site/wwwroot/numpy/__init__.py", line 109, in _delvewheel_patch_1_5_1 os.add_dll_directory(libs_dir)
i1icjdpr

i1icjdpr1#

为了在Azure Function应用中正确安装Numpy,请在requirements.txt中使用**numpy==1.25.0来正确安装Numpy,如下所示:
关于numpy库的类似问题,请参考我的
SO线程答案**

requirements.txt:-

numpy==1.25.0
pandas
azure-functions

init.py:-

import os
import sys
import azure.functions as func
import numpy as np
import pandas as pd

# Add the directory containing numpy DLLs to the DLL search path
dll_dir = os.path.join(os.path.dirname(np.__file__), 'DLLs')
os.add_dll_directory(dll_dir)

def main(req: func.HttpRequest) -> func.HttpResponse:
    try:
        # Your code that uses numpy and pandas here
        data = {'Column1': [1, 2, 3], 'Column2': [4, 5, 6]}
        df = pd.DataFrame(data)

        result = df.to_json()
        
        return func.HttpResponse(f"Data: {result}", mimetype="application/json")
    except Exception as e:
        return func.HttpResponse(f"An error occurred: {str(e)}", status_code=500)

  • Http触发器已正确部署,如下所示:-*

我的应用文件已导入numpy==1.25.0:-

相关问题