Pythonnet์ผ๋ก ์ฝ๋๋ฅผ ์์ฑํ ๋ ๊ฐ์ฅ ๋ถ๋ง์ธ ๋ถ๋ถ์ ์๋ฌด๋๋ ๊ธฐ์กด์ C# ์ฝ๋์ ๊ฒฐ์ด ๋ง์ง ์๊ฒ ์์ฑํ ์ ๋ฐ์ ์๋ค๋ ์ ์ ๋๋ค. ๊ทธ๋ฐ๋ฐ FBA ๋๋ถ์ ๊ธฐ์กด ํ๋ก์ ํธ ์ฝ๋๋ฅผ ์นจ๋ฒํ๊ฑฐ๋, ํ๋ค๊ฒ ํตํฉํ์ง ์๊ณ Python ํน์ ์ ์คํฌ๋ฆฝํธ ์ฝ๋๋ฅผ ๊ทธ๋๋ก ์กด์คํ๋ฉด์ ๋ ๋ฆฝ๋ ์ฝ๋๋ฅผ ์์ฑํ ์ ์๊ฒ ๋๋ ์ ์ ์ ๋ง ํ๋ฅญํฉ๋๋ค.
Python DLL์ ์ฐพ์์ฃผ๋ ๋ถ๋ถ๋ง ์ ํํ ์ฒ๋ฆฌํ ์ ์๋ค๋ฉด, ์๋์ ๊ฐ์ด Bootstrapper ์ฝ๋๋ฅผ ์์ฐ์ค๋ฝ๊ฒ ๋ง๋ค ์ ์์ด ๋งค์ฐ ์ ์ฉํ ๊ฒ ๊ฐ์ต๋๋ค.
#:package pythonnet@3.0.5
#:property PublishAot=False
using Python.Runtime;
var versionPostfix = "312";
Runtime.PythonDLL = Path.Combine(
Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData),
@$"Programs\Python\Python{versionPostfix}\python{versionPostfix}.dll");
RuntimeData.FormatterType = typeof(NoopFormatter);
try
{
PythonEngine.Initialize();
using (var gil = Py.GIL())
{
dynamic np = Py.Import("numpy");
Console.WriteLine(np.cos(np.pi * 2));
dynamic sin = np.sin;
Console.WriteLine(sin(5));
double c = (double)(np.cos(5) + sin(5));
Console.WriteLine(c);
dynamic a = np.array(new List<float> { 1, 2, 3 });
Console.WriteLine(a.dtype);
dynamic b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32);
Console.WriteLine(b.dtype);
Console.WriteLine(a * b);
}
}
finally
{
PythonEngine.Shutdown();
}