I am currently using PythonNet to connect C# and Python.
I know there is a way to call the function in Python script like below code.
try
{
PythonEngine.Initialize();
using (Py.GIL())
{
dynamic lb = Py.Import("parameter");
dynamic result = lb.target_function(1, 2);
}
}
catch(PythonException ex)
{
Console.WriteLine(ex.Message);
}
But is there any other code to call specific function without make the python script as Python library?
I want to run Python code through an absolute path like the code below.
Also, want to run specific function in the python code while passing parameters, like the code above.
try
{
PythonEngine.Initialize();
using (Py.GIL())
{
using (PyModule scope = Py.CreateScope())
{
scope.Exec(
File.ReadAllText(@"C:UsersDesktoptest.py")
);
}
PythonEngine.Shutdown();
//Console.WriteLine("Press any key...");
//Console.ReadKey();
}
}
catch(PythonException ex)
{
Console.WriteLine(ex.Message);
}
Specific code to try out for passing parameter from C# to Python using PythonNet but not running whole script but running only one function.