Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
219 views
in Technique[技术] by (71.8m points)

Use python functions in C# by IronPython (error)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[3] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

56.9k users

...