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