将python模块转换为DLL文件

hem*_*735 6 c# python wpf dll numpy

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

  1. 处理p = new Process(); //通过传递必要的参数,如"python.exe"和"filename.py"
  2. 使用IronPython

使用方法#1非常简单但是如果我分发这个应用程序,那么我的最终用户必须在我的应用程序中指定的路径中包含那些python模块.使用方法#2我遇到了这些python模块的问题,因为它们没有IronPython,我需要这些模块的DLL文件.那么有没有办法将像numpy,scipy这样的Python模块转换成DLL文件?

Cla*_*diu 3

您可以使用 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)
Run Code Online (Sandbox Code Playgroud)

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

import clr
import sys

sys.path.append(r"C:\Path\To\Dll")
clr.AddReference("myDll.dll")
Run Code Online (Sandbox Code Playgroud)

但是我不知道如何访问该 dll 内的函数。按照这个,就可以导入了import moduleName。但这对我不起作用。

来源:[将文件添加到单个 dll] [参考 dll]