C 呼叫Python檔案執行

2022-01-11 05:02:49 字數 1143 閱讀 9423

我的電腦環境是使用.net framework4.5.1,如果在除錯過程中調不通請注意

(1)首先先說乙個簡單的功能,在c#**中執行python字串,內容如下:

(2)c#呼叫python檔案:

在當前目錄下新建乙個字尾名為py的檔案,檔名為amoutdisc.py內容如下

dicamt = 0.05

retamt =amt

if amt>25

: retamt = amt-(amt*dicamt)

該檔案定義了乙個python檔案,然後在c#**中呼叫該python檔案,給該檔案傳參,具體應該說給引數amt傳值,最後在**中獲取python檔案中的retamt的變數

值,具體**如下:

scriptruntime scriptruntime =python.createruntime();

scriptengine pytheng = scriptruntime.getengine("

python");

scriptsource scriptsource = pytheng.createscriptsourcefromfile("

amoutdisc.py");

scriptscope scope =pytheng.createscope();

"prodcount", convert.toint32("34343"));

scope.setvariable("

amt", convert.todecimal("

434"

)); scriptsource.execute(scope);

dynamic a = scope.getvariable("

retamt

");

結果如下:

呼叫成功.

C 呼叫Python檔案 執行Python方法

inronpython是一種在.net和mono上實現的python語言,使用inronpython就可以在.net環境中呼叫python 安裝inronpython py檔案 coding utf 8 import sys reload sys sys.setdefaultencoding utf...

C 呼叫Python檔案

今天試用了好幾種方法,最終只有乙個方法成功,現將今天的收穫和問題做個筆記 這個方法首先要在vs中安裝配置ironpython,且如果python指令碼中包含第三方模組,這個方法大多會報錯說找不到import的包 但有的也能成功 在網上也很難找到該問題的解決方法,所以這個方法比較使用於呼叫不包含第三方...

C 呼叫python檔案

環境 win10 vs2017 anaconda3 python3.6 注意 在debug模式有可能編譯錯誤,換成release就好。1.新增python3.6中對應的include和libs 複製乙份libs下的python36.lib,並重命名為python36 d.lib,新增到聯結器輸出 p...