将python模块转换为DLL文件

时间:2015-09-11 18:58:25

标签: c# python wpf numpy dll

我正在使用.NET Framework上的WPF构建GUI。我想使用C#运行python脚本(使用像numpy,scipy等模块)。 现在,我有两个选择。

  1. 通过传递必要的参数来处理p = new Process(); //,例如" python.exe"和" filename.py"
  2. 使用IronPython
  3. 使用方法#1 非常简单但是如果我分发这个应用程序,那么我的最终用户必须在我的应用程序中指定的路径中包含那些python模块。 使用方法#2 我遇到了这些python模块的问题,因为他们没有使用IronPython,我需要这些模块的DLL文件。 那么有没有办法将像numpy,scipy这样的Python模块转换成DLL文件?

1 个答案:

答案 0 :(得分:1)

您可以使用clr.CompileModules将python文件转换为dll文件。您可以通过执行以下操作将所有文件添加到单个dll中:

from System import IO
from System.IO.Path import Combine

def walk(folder):
  for file in IO.Directory.GetFiles(folder):
    yield file
  for folder in IO.Directory.GetDirectories(folder):
    for file in walk(folder): yield file

folder = IO.Path.GetDirectoryName(__file__)
all_files = list(walk(Combine(folder, 'moduleName')))

import clr
clr.CompileModules(Combine(folder, "myDll.dll"), *all_files)

然后,您可以通过执行以下操作添加对dll的引用:

import clr
import sys

sys.path.append(r"C:\Path\To\Dll")
clr.AddReference("myDll.dll")

但我不知道如何访问该dll中的函数。根据{{​​3}},您可以通过import moduleName导入它。这对我不起作用。

来源:this [add files to single dll]