A simple example of C # calling a Python script _c# tutorial

Source: Internet
Author: User
Tags python script

IronPython is a kind of in. NET and the Python implementation on Mono, launched by Microsoft's Jim Hugunin, is an Open-source project based on Microsoft's DLR engine. IronPython's homepage on CodePlex:http://ironpython.codeplex.com/

Usage scenarios:

If your partner writes Python scripts, and the functionality of most of your projects has been implemented, you don't need to implement them in C #. Now the form is missing, at this point the combination of python+c# can be a perfect ending problem!

Example:
By IronPython, you can take advantage of it. NET executes code snippets stored in a Python script. The following is a simple example that shows how to apply C # to invoke a Python script.

1. Create a new form item in VS: Ironpythondemo

2, vs Menu Open "NuGet Package Manager"

3, search IronPython package and install

4, in the EXE program in the folder (in this case, ". \ironpythondemo\ironpythondemo\bin\debug"), create a Python script. or copy the existing script to the directory. The Python sample script implements a two-number arithmetic:

Num1=arg1 
num2=arg2 
op=arg3 
if op==1: result=num1+num2 elif op==2 
: 
 result=num1-num2 
Elif op==3: 
 result=num1*num2 
Else: 
 result=num1*1.0/num2 

5, modify the project configuration file app.config as follows:
several properties of the IronPython language engine are set in the Microsoft.scripting node.

<?xml version= "1.0" encoding= "Utf-8"?> 
<configuration> 
 <configSections> 
 < Section name= "microsoft.scripting" type= "Microsoft.Scripting.Hosting.Configuration.Section, microsoft.scripting"/ > 
 </configSections> 
 <microsoft.scripting> 
 <languages> 
  <language names= " IronPython; Python;py "extensions=". Py "Displayname=" Python "type=" IronPython.Runtime.PythonContext, IronPython "/> 
 < /languages> 
 </microsoft.scripting> 
 <startup> 
  <supportedruntime version= "v4.0" Sku= ". netframework,version=v4.5 "/> 
 </startup> 

6. The drawing form is as follows:

7, write the calculation function:

private void Btncalculate_click (object sender, EventArgs e) 
  { 
   Scriptruntime scriptruntime = Scriptruntime.createfromconfiguration (); 
   ScriptEngine Rbeng = scriptruntime.getengine ("python"); 
   Scriptsource Source = Rbeng.createscriptsourcefromfile ("ironpythondemo.py");/Set script file 
   scriptscope scope = Rbeng.createscope (); 
 
   Try 
   { 
    //Set parameter 
    scope. SetVariable ("Arg1", Convert.ToInt32 (Txtnum1.text)); 
    Scope. SetVariable ("Arg2", Convert.ToInt32 (Txtnum2.text)); 
    Scope. SetVariable ("Arg3", operation. selectedindex+1); 
   } 
   catch (Exception) 
   { 
    MessageBox.Show ("entered incorrectly.) "); 
   } 
 
   Source. Execute (scope); 
   Labelresult.text = scope. GetVariable ("result"). ToString (); 
  } 

8, the compilation run may calculate the result (here does not do the input inspection)

The above is the C # call Python script simple method, hope to be helpful to everybody's study.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.