Error while deploying python code to linux based azure function

1 day ago 6
ARTICLE AD BOX

I am trying to deploy python code to azure function (linux based, python 3.12).However, I am getting the following error on azure:

> ImportError: Unable to import required dependencies: numpy: Error > importing numpy: you should not try to import numpy from > its source directory; please exit the numpy source tree, and relaunch > your python interpreter from there. Cannot find module. Please check the requirements.txt file for the missing module. For more info,

Please find below python code and other dependent files:

function_app.py

import azure.functions as func import logging import json import pandas import numpy import statsmodels.api as sm from scipy import stats @app.route(route="health_check", methods=["GET"]) async def health_check(req: func.HttpRequest) -> func.HttpResponse: # Basic usage of pandas df = pandas.DataFrame({'A': [1, 2], 'B': [3, 4]}) pandas_result = df.sum().to_dict() print(pandas_result) # Basic usage of numpy np_result = float(numpy.mean([1, 2, 3])) print(np_result) # Basic usage of statsmodels X = [1, 2, 3] y = [2, 4, 6] X_const = sm.add_constant(X) model = sm.OLS(y, X_const) results = model.fit() sm_result = float(results.params[1]) # Slope print(sm_result) # Basic usage of scipy scipy_result = float(stats.pearsonr(X, y)[0]) # Correlation print(scipy_result) result = { "pandas_sum": pandas_result, "numpy_mean": np_result, "statsmodels_slope": sm_result, "scipy_correlation": scipy_result } return func.HttpResponse(json.dumps(result), status_code=200, mimetype="application/json")

requirements.txt

azure-functions pandas==2.3.2 numpy==2.2.6 statsmodels==0.14.5 scipy==1.15.3

I am using windows so I build packages locally using the following command:

py -m pip install --target=".python_packages/lib/site-packages" -r requirements.txt --only-binary=:all: --upgrade --platform manylinux2014_x86_64

and create a zip file using the following files/folder:

function_app.py host.json requirements.txt .funcignore .python_packages

Am I missing any steps?

Read Entire Article