C # Call the python file for execution,

Source: Internet
Author: User

C # Call the python file for execution,

My computer environment uses. net framework4.5.1. Please note that

I'm using Visual studion 2017, python components: http://ironpython.codeplex.com/releases/view/

The downloaded version is 2.7. After the download and installation, remember to introduce the following three dll files in the installation path.

(1) First, let's talk about a simple function. Execute a python string in the c # code. The content is as follows:

 

(2) c # Call the python file:

Create a new file with the suffix "py" in the current directory. The file name is AmoutDisc. py and the content is as follows:

dicAmt = 0.05retAmt = amtif amt>25:   retAmt = amt-(amt*dicAmt)

This file defines a python file, and then calls the python file in the c # code to pass parameters to the file. It should be said that the parameter amt is used to pass values, finally, get the retAmt variable in the python file in the code.

The Code is as follows:

                ScriptRuntime scriptRuntime =Python.CreateRuntime();                ScriptEngine pythEng = scriptRuntime.GetEngine("Python");                ScriptSource scriptSource = pythEng.CreateScriptSourceFromFile("AmoutDisc.py");                ScriptScope scope = pythEng.CreateScope();                //scope.SetVariable("prodCount", Convert.ToInt32("34343"));                scope.SetVariable("amt", Convert.ToDecimal("434"));                scriptSource.Execute(scope);                dynamic a = scope.GetVariable("retAmt");

The result is as follows:

The call is successful.

 

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.