C # invokes Python file execution

Source: Internet
Author: User

My Computer environment is using. net framework4.5.1, if it doesn't work during debugging please note

I'm using the visual studion 2017,python component: http://ironpython.codeplex.com/releases/view/

The downloaded version is 2.7, and after the download installs, remember to introduce the following three DLLs under the installation path

(1) first of all, a simple function, in C # code to execute the Python string, the content is as Follows:

(2) C # call Python file:

In the current directory, create a new suffix named py file, the file name is amoutdisc.py content as follows

0.05= amtif amt>:   = Amt-(amt*dicamt)

The file defines a Python file, then calls the Python file in C # code, gives the file a parameter, and, in particular, passes the parameters to the amt, and finally gets the Retamt variable in the Python file in the Code.

value, The following code is specified:

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); DynamicA = Scope. GetVariable ("Retamt");

The results are as follows:

The call Succeeded.

C # invokes Python file execution

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.