简体   繁体   English

如何将python模块及其依赖项(例如Numpy / Scipy)打包到一个文件中,例如dll以进行C#调用?

[英]How can I package my python module and its dependencies such as Numpy/Scipy into one file like dll for C# calls?

In my project, some of the algorithms are implemented in python as Modules for calling and libraries such as Numpy/Scipy are used . 在我的项目中,某些算法在python中实现为调用模块,并使用了诸如Numpy / Scipy之类的库。 Now I need to embed these Modules into my UI(implemented by C# and run in Windows 7). 现在,我需要将这些模块嵌入到我的UI中(由C#实现并在Windows 7中运行)。 There are two reasons I need to package my python modules into a file like DLL(I don't want to package as an .exe because it is not friendly to embed). 有两个原因需要将python模块打包到DLL之类的文件中(我不想打包为.exe,因为它不易于嵌入)。 The first reason is for easily calling, and the second reason is to protect my algorithm source code. 第一个原因是易于调用,第二个原因是保护我的算法源代码。 Does anyone have any idea? 有人有什么主意吗?

Finally, I solve my problem by using nuitka. 最后,我使用nuitka解决了我的问题。 It compile my module into .pyd file, and I can import the module using pythonnet by adding reference Python.Runtime.dll. 它将我的模块编译成.pyd文件,并且可以通过添加参考Python.Runtime.dll使用pythonnet导入模块。 My compile command is: nuitka --module --nofreeze-stdlib --recurse-all --recurse-not-to=numpy --recurse-not-to=sklearn --recurse-not-to=pywt --recurse-not-to=matplotlib --recurse-not-to=matplotlib.pyplot --recurse-not-to=scipy.io myfile.py and my C# code is: 我的编译命令是: nuitka --module --nofreeze-stdlib --recurse-all --recurse-not-to=numpy --recurse-not-to=sklearn --recurse-not-to=pywt --recurse-not-to=matplotlib --recurse-not-to=matplotlib.pyplot --recurse-not-to=scipy.io myfile.py ,我的C#代码为:

PythonEngine.Initialize();
IntPtr pythonLock = PythonEngine.AcquireLock();
PythonEngine.RunSimpleString("import sys\n");
PythonEngine.RunSimpleString("sys.path.append('C:\\path\\to\\pyd')\n");
PyObject pModule = PythonEngine.ImportModule("modulename");
pModule.InvokeMethod("funcitonname", para1, para2);

Try pythonnet to embed CPython in .NET: 尝试使用pythonnet将CPython嵌入.NET:

python4.net python4.net

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将所有.dll文件和.exe文件打包为C#文件,就像Java关于Jar一样 - How to package all .dll files and .exe into one file in C#, just like Java does about Jar 我如何在一个DLL中拥有C#表单资源,而实现代码却在另一个DLL中? - How can I have my C# Form resources in one DLL, whilst the implementing code is in another DLL? C#:如何在一个文件中读取 'éóú' 和 öäü 等字符? - C#: How can I read characters like 'éóú' and öäü in one file? 如何解决C#中的循环依赖关系? - How can I resolve my circular dependencies in c#? 如何使用DLL在C#代码中读取Excel 2010文件? - How can I read an Excel 2010 file in my C# code using a DLL? Visual C#“无法加载文件或程序集或其依赖项之一” - Visual C# “Could not load file or assembly or one of its dependencies” 如何在DLL中使用C#锁定对象 - How can I lock my object using C# in dll 无法加载文件或程序集“ XXXX.dll”或其依赖项之一。 指定的模块无法找到 - Could not load file or assembly 'XXXX.dll' or one of its dependencies. The specified module could not be found 无法加载文件或程序集“xxx.dll”或其依赖项之一。 指定的模块无法找到 - Could not load file or assembly 'xxx.dll' or one of its dependencies. The specified module could not be found 无法加载文件或程序集“ idrsnet15.dll”或其依赖项之一。 指定的模块无法找到 - Could not load file or assembly 'idrsnet15.dll' or one of its dependencies. The specified module could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM