8

I am building a GUI using WPF on .NET Framework.I want to run python script (which uses modules like numpy, scipy ,etc) using C#. Now, I have two options.

  1. Process p = new Process(); //by passing necessary parameters like "python.exe" and "filename.py"
  2. Using IronPython

Using Method#1 is pretty much simpler but If I distribute this Application,then my End-User must have those python modules in the path specified by me in my Application. Using Method#2 I am facing problems with those python modules because they don't come with IronPython and I need DLL files of those modules. So is there any way to Convert Python Modules like numpy, scipy into DLL files?

1 Answer 1

3

You can use clr.CompileModules to turn the python files into dll ones. You can add all the files into a single dll by doing something like this:

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)

You can then add a reference to your dll by doing something like this:

import clr
import sys

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

I don't however know how to access the functions inside that dll. According to this, you can import it by doing import moduleName. This does not work for me though.

Sources: [add files to single dll] [reference dll]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.