Using ILMerge with IronPython

420 Views Asked by At

I have trouble merging IronPython.dll, IronPython.Modules.dll, Microsoft.Dynamic.dll, Microsoft.Scripting.dll and Microsoft.Scripting.Metadata.dll into my application.

The first error i got while trying to execute a python script was:

MissingMemberException: "'NullImporter' object has no attribute 'find_module'"

This was resolved by omitting the /internalize Parameter of ILMerge. It seems that IronPython needs certain types to be public in order to function.

But it didn't help much, now i got:

ImportException: "No module named clr"

The exception in both cases is thrown for the first line of my script, which of course is just an "import clr".

1

There are 1 best solutions below

1
On BEST ANSWER

Sadly, it seems like extremely dynamic runtimes such as IronPython are going to be the least cooperative when it comes to working after an ILMerge.

You might consider doing some of the assembly embedding tricks that single-exe projects like LINQPad do.

  1. Embed all of the third-party assemblies that you depend on in your application's Resources.
  2. Register a ResolveEventHandler with the AppDomain.CurrentDomain.AssemblyResolve event.
  3. When your handler gets called with an assembly that you stashed in Resources, load the assembly.

You do part 3 as follows:

var resourceStream = Assembly.GetExecutingAssembly().GetManifestResourceStream(name);
return Assembly.Load(new BinaryReader(resourceStream).ReadBytes(int.MaxValue));

If you need more help, you can poke around in LINQPad.exe and look at LinqPad.Program.AddLINQPadAssemblyResolver() and LinqPad.Program.FindAssem().

Update: Just found a blog post by Jeffrey Richter that gives more details on this approach.