C#NOTE13: How to call Python in C #

Source: Internet
Author: User

Preface

IronPython is a Python implementation on. NET and Mono, launched by Microsoft's Jim Hugunin (also the Jython creator) and is an open source project based on the Microsoft DLR engine. In 2007, developers decided to rewrite the architecture and use the dynamic type system to make it easy for more scripting languages to be ported to the NET Framework. IronPython's official did not implement the Python Universal class Library, only implemented some core classes, the community's Open source class library implementation has:

Fepy (http://fepy.sourceforge.net/): Fepy provides the implementation of most common class libraries for Python for IronPython.

Test for IronPython

(1) Create a new form project in VS2017: Ironpythontest.

(2) The "NuGet Package Manager" opens in the VS menu bar tool:

(3) Search for the IronPython package and install:

(4) After the installation succeeds, the Python script can be created under the folder where the EXE program is located (or in other directories, by specifying a relative path):

Example (arithmetic for two numbers)

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 configuration file for the Project App. Config:

(6) Calculate the button's Click event:

private void Btncalculate_click (object sender, EventArgs e)        {            Scriptruntime scriptruntime = Scriptruntime.createfromconfiguration ();            ScriptEngine Rbeng = scriptruntime.getengine ("python");            Scriptsource Source = Rbeng.createscriptsourcefromfile ("hello.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 ("Input is incorrect. ");            }            Source. Execute (scope);            Labelresult.text = scope. GetVariable ("result"). ToString ();        }

(7) Operation Result:

Refer to related articles:

Using IronPython to integrate Python and. NET

Cross-language and cross-compiler of those pits (CPython vs IronPython)

C#NOTE13: How to call Python in C #

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.