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.