由于这些限制,我发现了Pythonnet,它也有助于将.net应用程序与Python集成.但是我是新手,所以想要一些实现相同的想法以及可用的代码示例,并且建议将其与Python 3.7一起使用是否可行或建议使用我检查了最初设置Pythonnet的麻烦,因此需要帮助或有关如何设置Pythonnet的步骤.还想知道将来Iron Iron Python是否也将支持Python 3.X.解决方案我对IronPython并不熟悉,但出于相同的目的,我大量使用了 pythonnet -将Python与C#集成在一起,所以我可以详细说明.使用 pythonnet 的优势是使所有CPython软件包可供您使用(numpy,scipy,pandas,Theano,Keras,scikit-learn等),但避免了开销python.exe作为单独的进程调用(pythonnet通过将pythonXY.dll加载到您的进程中来工作).请注意,pythonnet还需要具有独立的Python,但您可以使用可嵌入的Python软件包,它非常轻巧,可以与您的应用程序一起分发. pythonnnet 支持Python 3.7,但已发布的NuGet软件包仅适用于Python 3.5.您有多种选择来获取适用于Python 3.7的pythonnet:从PyPi下载 pythonnet wheel 程序包,然后从其中提取 Python.Runtime.dll按照 pythonnet安装Wiki从源代码构建 重要说明: pythonnet 版本必须与您的 Python版本和位数相匹配.例如,如果您使用的是 32位Python 3.7 ,请下载 pythonnet-2.4.0-cp37-cp37m-win32.whl .如果您的 Python是64位,请下载 pythonnet-2.4.0-cp37-cp37m-win_amd64.whl .您的 C#项目平台目标也必须匹配(对于32位是x86,对于64位是x64).具有与您发布的功能类似的功能的代码示例,使用pythonnet(已在Windows 7上使用Python 3.7.4进行了测试,并通过最新的构建工件): private void Test() { // Setup all paths before initializing Python engine string pathToPython = @"C:\Users\user\AppData\Local\Programs\Python\Python37-32"; string path = pathToPython + ";" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine); Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONHOME", pathToPython, EnvironmentVariableTarget.Process); var lib = new[] { @"C:\Users\user\source\path\repos\SamplePy\SamplePy2", Path.Combine(pathToPython, "Lib"), Path.Combine(pathToPython, "DLLs") }; string paths = string.Join(";", lib); Environment.SetEnvironmentVariable("PYTHONPATH", paths, EnvironmentVariableTarget.Process); using (Py.GIL()) //Initialize the Python engine and acquire the interpreter lock { try { // import your script into the process dynamic sampleModule = Py.Import("SamplePy"); // It is more maintainable to communicate with the script with // function parameters and return values, than using argv // and input/output streams. int x = 3; int y = 4; dynamic results = sampleModule.sample_func(x, y); Console.WriteLine("Results: " + results); } catch (PythonException error) { // Communicate errors with exceptions from within python script - // this works very nice with pythonnet. Console.WriteLine("Error occured: ", error.Message); } } } SamplePy.py:def sample_func(x, y): return x*yI want to integrate Python with C#. I found two approaches using Interprocess communication and IronPythonInterprocess communication requires Python.exe to be installed on all client machines so not a viable solution.We started using IronPython, but it only supports 2.7 python version for now. We are using 3.7 version.Following code we tried using IronPython:private void BtnJsonPy_Click(object sender, EventArgs e) { // 1. Create Engine var engine = Python.CreateEngine(); //2. Provide script and arguments var script = @"C:\Users\user\source\path\repos\SamplePy\SamplePy2\SamplePy2.py"; // provide full path var source = engine.CreateScriptSourceFromFile(script); // dummy parameters to send Python script int x = 3; int y = 4; var argv = new List<string>(); argv.Add(""); argv.Add(x.ToString()); argv.Add(y.ToString()); engine.GetSysModule().SetVariable("argv", argv); //3. redirect output var eIO = engine.Runtime.IO; var errors = new MemoryStream(); eIO.SetErrorOutput(errors, Encoding.Default); var results = new MemoryStream(); eIO.SetOutput(results, Encoding.Default); //4. Execute script var scope = engine.CreateScope(); var lib = new[] { "C:\\Users\\user\\source\\repos\\SamplePy\\CallingWinForms\\Lib", "C:\\Users\\user\\source\\repos\\SamplePy\\packages\\IronPython.2.7.9\\lib", "C:\\Users\\user\\source\\repos\\SamplePy\\packages\\IronPython.2.7.9", "C:\\Users\\user\\source\\repos\\SamplePy\\packages\\IronPython.StdLib.2.7.9" //"C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37 - 32\\Lib", //"C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\python.exe", //"C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37 - 32", //"C:\\Users\\user\\AppData\\Local\\Programs\\Python\\Python37-32\\DLLs" }; engine.SetSearchPaths(lib); engine.ExecuteFile(script, scope); //source.Execute(scope); //5. Display output string str(byte[] x1) => Encoding.Default.GetString(x1); Console.WriteLine("Errrors"); Console.WriteLine(str(errors.ToArray())); Console.WriteLine(); Console.WriteLine("Results"); Console.WriteLine(str(results.ToArray())); lblAns.Text = str(results.ToArray()); }The problem sometimes is, to do heavy Machine Learning programming we need to add "Modules". These Modules would be dependent on other modules. This increases point 4, Execute Scripts part of code, as more data path of that module has to be given here var lib = new[] and also some modules are not supported with Iron Python as well (for e.g. modules concerning OCR operations etc.)Due to these limitations I found Pythonnet which also helps in integrating .net applications with Python. But I am new to it, so want some ideas on implementing the same, and code samples available, and is it feasible or recommended to use with Python 3.7I checked that setting up Pythonnet is cumbersome initially, so want help or steps on how to set up the same. Also would like to know if in future Iron Python would support Python 3.X as well or not. 解决方案 I am not familiar with IronPython, but I use pythonnet quite a lot for the same purpose - integrate Python with C#, so I can elaborate on that.The advantage of using pythonnet for your purposes is having all the CPython packages available for you to use (numpy, scipy, pandas, Theano, Keras, scikit-learn etc), but avoiding the overhead of calling python.exe as separate process (pythonnet works by loading pythonXY.dll into your process).Pay attention that pythonnet also requires to have stand-alone Python availiable, but you can use Embeddable Python package which is very light-weight and can be distributed with your application.pythonnnet supports Python 3.7, but the published NuGet packages are only for Python 3.5. You have several choices to obtain pythonnet for Python 3.7:Download pythonnet wheel package from PyPi and extract Python.Runtime.dll from itDownload NuGet package from pythonnet appveyor build artifacts, as advised on pythonnet installation wikiBuild from sourcesImportant note: pythonnet version has to match your Python version and bitness. For example, if you are using Python 3.7 32-bit, download pythonnet-2.4.0-cp37-cp37m-win32.whl. If your Python is 64-bit, download pythonnet-2.4.0-cp37-cp37m-win_amd64.whl. Your C# project platform target also has to match (x86 for 32-bit or x64 for 64-bit).Code sample with similar functionality to what you have posted, using pythonnet (tested with Python 3.7.4 on Windows 7 and pythonnet NuGet from latest build artifacts): private void Test() { // Setup all paths before initializing Python engine string pathToPython = @"C:\Users\user\AppData\Local\Programs\Python\Python37-32"; string path = pathToPython + ";" + Environment.GetEnvironmentVariable("PATH", EnvironmentVariableTarget.Machine); Environment.SetEnvironmentVariable("PATH", path, EnvironmentVariableTarget.Process); Environment.SetEnvironmentVariable("PYTHONHOME", pathToPython, EnvironmentVariableTarget.Process); var lib = new[] { @"C:\Users\user\source\path\repos\SamplePy\SamplePy2", Path.Combine(pathToPython, "Lib"), Path.Combine(pathToPython, "DLLs") }; string paths = string.Join(";", lib); Environment.SetEnvironmentVariable("PYTHONPATH", paths, EnvironmentVariableTarget.Process); using (Py.GIL()) //Initialize the Python engine and acquire the interpreter lock { try { // import your script into the process dynamic sampleModule = Py.Import("SamplePy"); // It is more maintainable to communicate with the script with // function parameters and return values, than using argv // and input/output streams. int x = 3; int y = 4; dynamic results = sampleModule.sample_func(x, y); Console.WriteLine("Results: " + results); } catch (PythonException error) { // Communicate errors with exceptions from within python script - // this works very nice with pythonnet. Console.WriteLine("Error occured: ", error.Message); } } }SamplePy.py:def sample_func(x, y): return x*y 这篇关于Python和.Net集成选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
09-05 10:45
查看更多