I am trying to use my simple python class in C# windows form App by using IronPython.
I succeed to use that but after import a random library in python scrip the engine start to gives me an error.
So how can I import this library to the C# code ?!
Note my problem start after adding this line to python code ( import random )
My Python code :
import random
class Math:
def RandomF(self,a,b):
return random.randint(a,b)
def Emptyss(self):
return 3
def sum(self,a,b):
return a+b
def sub(self,a,b):
return a-b
def mult(self,a,b):
return a*b
def devi(self,a,b):
return a/b
def Max(self,a,b):
if a >b:
return a
else:
return b
def Min(self,a,b):
if a<b:
return a
else :
return b
My C# class :
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Speech;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Windows.Forms;
using System.IO;
using IronPython.Hosting;
using Microsoft.Scripting.Hosting;
namespace VoiceRecognition
{
class VoiceRecongnitionClass
{
Microsoft.Scripting.Hosting.ScriptEngine engine;
dynamic MathClass;
public VoiceRecongnitionClass()
{
try
{
string code = @"import random
class Math:
def RandomF(self,a,b):
return random.randint(a,b)
def Emptyss(self):
return 3
def sum(self,a,b):
return a+b
def sub(self,a,b):
return a-b
def mult(self,a,b):
return a*b
def devi(self,a,b):
return a/b
def Max(self,a,b):
if a >b:
return a
else:
return b
def Min(self,a,b):
if a<b:
return a
else :
return b
";
engine = Python.CreateEngine();
dynamic scope = engine.CreateScope();
//error start here
engine.Execute(code, scope);
MathClass = scope.Math();
}
catch (Exception a)
{
MessageBox.Show(a.ToString());
}
}
public float sum(float a,float b)
{
return MathClass.sum(a,b);
}
}
}
Error message :
IronPython.Runtime.Exceptions.ImportException: No module named random
at Microsoft.Scripting.Runtime.LightExceptions.CheckAndThrow(Object value)
at Microsoft.Scripting.Interpreter.FuncCallInstruction`2.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
at Microsoft.Scripting.Interpreter.LightLambda.Run2[T0,T1,TRet](T0 arg0, T1 arg1)
at IronPython.Compiler.PythonScriptCode.RunWorker(CodeContext ctx)
at IronPython.Compiler.PythonScriptCode.Run(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.InvokeTarget(Scope scope)
at IronPython.Compiler.RuntimeScriptCode.Run(Scope scope)
at Microsoft.Scripting.SourceUnit.Execute(Scope scope, ErrorSink errorSink)
at Microsoft.Scripting.Hosting.ScriptSource.Execute(ScriptScope scope)
at Microsoft.Scripting.Hosting.ScriptEngine.Execute(String expression, ScriptScope scope)
at CallSite.Target(Closure , CallSite , ScriptEngine , String , Object )
at System.Dynamic.UpdateDelegates.UpdateAndExecuteVoid3[T0,T1,T2](CallSite site, T0 arg0, T1 arg1, T2 arg2)
at VoiceRecognition.VoiceRecongnitionClass..ctor() in D:VisualStudio_ProjectsProjectsVoiceRecognitionVoiceRecognitionVoiceRecognitionVoiceRecongnitionClass.cs:line 78
Not : line 78 is this "engine.Execute(code, scope);"
question from:
https://stackoverflow.com/questions/65865082/use-python-functions-in-c-sharp-by-ironpython-error