본문 바로가기
SoftWare/Visual C#

파이썬 c# 연동 IronPython

by 학수씨 2021. 11. 28.

var engine = IronPython.Hosting.Python.CreateEngine();
var scope = engine.CreateScope();
var paths = engine.GetSearchPaths();

/*
paths.Add(@".\python");
paths.Add(@".\python\venv");
paths.Add(@".\python\venv\Lib");
paths.Add(@".\python\venv\Lib\site-packages");
paths.Add(@"D:\OneDrive\2.CREEDSOFT\1.PrintCheckVision\Source\LineTrigger\LineTrigger\Lib");
*/


paths.Add(@"C:\Python27");
paths.Add(@"C:\Python27\DLLs");
paths.Add(@"C:\Python27\Lib");
paths.Add(@"C:\Python27\Lib\site-packages");



engine.SetSearchPaths(paths);

try
{
	//var source = engine.CreateScriptSourceFromFile(@".\python\main.py");
	var source = engine.CreateScriptSourceFromFile(@".\python\test.py");
	source.Execute(scope);

	var getPythonFuncResult = scope.GetVariable<Func<string>>("getPythonFunc");
	Console.WriteLine("def 실행 테스트 : " + getPythonFuncResult());

	var sum = scope.GetVariable<Func<int, int, int> > ("sum");
	Console.WriteLine(sum(1, 2));

	var test_arry = scope.GetVariable<Func<byte[], IronPython.Runtime.List>>("test_arry");
	IronPython.Runtime.List list = new IronPython.Runtime.List();
	list = test_arry(new byte[4] { 0, 1, 2, 3 });


}
catch (Exception ex)
{
	
}
import numpy as np

def getPythonFunc():
    return "hello"


def sum(a, b):
    c = a + b
    return c


def test_arry(test_list):
    for i in test_list:
        print(i)

    rst_arry = []

    rst_arry.append("1");
    rst_arry.append("2");
    rst_arry.append("3");
    rst_arry.append("4");

    return rst_arry




print("hello iron python")

'SoftWare > Visual C#' 카테고리의 다른 글

C# <-> JSP (Tomcat) 간 파일 업로드  (0) 2018.09.08
C# RTSP Client Demo  (2) 2017.08.12
C# 우선순위 큐  (0) 2014.09.20
병렬처리  (0) 2013.03.18
C# TextBox 컨트롤+A Ctrl+A 동작하게 하기..  (0) 2012.09.13

댓글