赞
踩
C# 用pythonnet 嵌入Python代码,可以导入第三方库和自己python代码
conda create --prefix=D:\condaenv\env_name python=3.10
conda activate F:\condaenv\env_name
using Python.Runtime; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.IO; string pathToVirtualEnv = "D:\\condaenv\\env_name"; //string pathToVirtualEnv = "E:\\anaconda3"; Environment.SetEnvironmentVariable("PATH", pathToVirtualEnv, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib;E:\\anaconda3\\Library\\bin;E:\\anaconda3\\Library\\usr\\bin;E:\\anaconda3\\Scripts", EnvironmentVariableTarget.Process); Runtime.PythonDLL = "D:\\condaenv\\env_name\\python310.dll"; //Runtime.PythonDLL = "E:\\anaconda3\\python310.dll"; PythonEngine.PythonHome = pathToVirtualEnv; PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process); PythonEngine.Initialize(); using (Py.GIL()) { dynamic np = Py.Import("numpy"); //dynamic np1 = Py.Import("test3"); //Console.WriteLine(np1.Add(2)); //Console.WriteLine(np.cos(np.pi * 2)); //py.hello(); double c = (double)(np.array(1)); Console.WriteLine(c); }
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using Python.Runtime; namespace DemoPythonnet { public partial class Form1 : Form { public Form1() { InitializeComponent(); } public dynamic np = null; public dynamic a; public dynamic b; private void Form1_Load(object sender, EventArgs e) { string pathToVirtualEnv = "D:\\condaenv\\env_name"; Environment.SetEnvironmentVariable("PATH", pathToVirtualEnv, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONHOME", pathToVirtualEnv, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONPATH", $"{pathToVirtualEnv}\\Lib\\site-packages;{pathToVirtualEnv}\\Lib", EnvironmentVariableTarget.Process); Runtime.PythonDLL = "D:\\condaenv\\env_name\\python310.dll"; PythonEngine.PythonHome = pathToVirtualEnv; PythonEngine.PythonPath = PythonEngine.PythonPath + ";" + Environment.GetEnvironmentVariable("PYTHONPATH", EnvironmentVariableTarget.Process); PythonEngine.Initialize(); np = Py.Import("numpy"); } private void button1_Click(object sender, EventArgs e) { //使用第三方库 using (Py.GIL()) { 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); a = np.array(new List<float> { 1, 2, 3 }); Console.WriteLine(a.dtype); b = np.array(new List<float> { 6, 5, 4 }, dtype: np.int32); Console.WriteLine(b.dtype); Console.WriteLine(a * b); } } private void button2_Click(object sender, EventArgs e) { using (Py.GIL()) { Console.WriteLine(a * b); } } } }
感谢 https://blog.csdn.net/The_Moon_/article/details/127208124
感谢 https://blog.csdn.net/weixin_44064908/article/details/129270649
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。