0

we have created an Azure function in the dotnet framework(.NET6) that processes some python scripts in runtime using pythonnnet which was embedded inside Numpy(3.7.1.28) - Nuget package.

Locally everything works fine(VS 2022, Windows 10). But once deployed in azure functions (Windows Container) we are getting the following Error. Exception: Could not load file or assembly 'Python.Runtime, Version=2.5.1.0, Culture=neutral, PublicKeyToken=null'. Could not find or load a specific file. Python version is 3.7.3(embedded) But that particular Python.Runtime dll is present when inspecting in Azure Kudu.

I am also looking for suggestions regarding combining both C# and python. Few options are IronPython and pythonnet. But in our scenario, we need the use 3rd party packages like Numpy, Pandas, and Scipy for engineering calculations.

Thanks in advance.

karmel
  • 1

1 Answers1

0

Locally:

I have used relative path and ScriptEngine and ScriptScope classes:

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;

namespace KrishNet6PyFunc101
{
    public static class Function1
    {
        private static ScriptScope pyscriptoutput;

        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            ScriptEngine engine = Python.CreateEngine();
            ScriptScope scope = engine.CreateScope();
            engine.ExecuteFile(".\\script.py", scope);
            var result = scope.GetVariable("joined_string");
            Console.WriteLine(result);



            string responseMessage = "Hello Krishna, This HTTP triggered function executed successfully and Python Script Output is " +result;
            return new OkObjectResult(responseMessage);
        }
    }
}

enter image description here

Azure Portal:

For the Azure Function App Portal deployed, I have changed the code accordingly and it worked:

Using the same ScriptEngine and ScriptScope classes but taken the path using FunctionAppDirectory,

using System;
using System.IO;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Azure.WebJobs;
using Microsoft.Azure.WebJobs.Extensions.Http;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Microsoft.Scripting.Hosting;
using IronPython.Hosting;

namespace KrishNet6PyFunc101
{
    public static class Function1
    {
        private static ScriptScope pyscriptoutput;

        [FunctionName("Function1")]
        public static async Task<IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", Route = null)] HttpRequest req,
            ILogger log,ExecutionContext context)
        {
            log.LogInformation("C# HTTP trigger function processed a request.");
            ScriptEngine engine = Python.CreateEngine();
            ScriptScope scope = engine.CreateScope();

            string lfile = Path.Combine(context.FunctionAppDirectory, "script.py");
            Console.WriteLine("filepath:"+lfile);
            engine.ExecuteFile(lfile,scope);
            var result = scope.GetVariable("joined_string");
            Console.WriteLine(result);


            string responseMessage = "Hello Krishna, This HTTP triggered function executed successfully and Python Script Output is " +result;
            return new OkObjectResult(responseMessage);
        }
    }
}

enter image description here