This article mainly for you in detail the C # Call Python module method, with a certain reference value, interested in small partners can refer to
Now, C # and Python are the most popular computer programming languages, they have their pros and cons, and if they can work with each other, it's a wonderful thing, today I'll explain how to use C # to invoke Python.
If you let C # support calling the Python module, we first need to install some extensions, and we recommend using the IronPython library.
The first step, we need to download IronPython Library installation package, here please move to the official website ironpython.codeplex.com/, download and install the relevant library files.
In the second step, we create a new C # console test project and add the following DLL file in the IronPython installation directory to the project reference.
In the third step, we write the C # code separately and add the Python code file to the project with the following code.
C # code section
Using system;using ironpython.hosting; Import IronPython library file using Microsoft.Scripting.Hosting; Import Microsoft Script Interpreter library file namespace consoleapplication1{class program { static void Main (string[] args) { Scriptruntime pyruntime = Python.createruntime (); Create a run Environment dynamic obj=pyruntime.usefile ("debug.py");//Call a python file int num1, num2; Console.Write ("NUM1:"); NUM1 = Convert.ToInt32 (Console.ReadLine ()); Console.Write ("Num2:"); num2 = Convert.ToInt32 (Console.ReadLine ()); int sum = Obj.add (NUM1, num2); Call the SUM function console.write in the Python file ("sum:"); Console.WriteLine (sum); } }}
Python Code section
def add (num1,num2): return num1+num2;
Compile-time needs to modify the project's Python file attributes, otherwise it will be reported that the file could not locate the error, the results of the operation as shown below.
Through the above experimental process, you can easily use C # to call Python files, but do not know whether people will and I have the same doubts, how to put C # executable program and Python file packaging? If you have a simple compression, you may be missing the library file you want to run, and if you have questions about it, please note the blog's next article: use Visual Studio to find the necessary runtime files for your program.
"Recommended"
1. Special recommendation : "PHP Programmer Toolkit" V0.1 version download
2. asp free Video Tutorial
3. Eon the ASP Basic video tutorial