[Python.NET] how can I build an assembly I can AddReference()?

Brandon Craig Rhodes brandon at rhodesmill.org
Fri May 28 06:10:01 CEST 2010


I am interested in whether Python for .NET can help me provide
third-party programmers with a way to write C# routines that will get
called from my Python application.  I was impressed earlier today with
how easily Python for .NET compiled for Python 2.6 and the most recent
Visual Studio release, so I set about trying to write a C# routine and
call it from inside of Python.

I have only gotten as far as trying to AddReference() my new DLL, but
have not been able to get it to succeed.

I first created a "Brandon.cs" file with this inside:

----------------------------------------
using System;
using System.Reflection;

[assembly:AssemblyVersion("1.0.0.0")]
[assembly:AssemblyDescription("The Brandon library")]
[assembly:AssemblyCompany("Rhodes Mill Studios, Inc.")]

namespace Brandon
{
   public static class BrandonClass
   {
      public static void Go()
      {
         Console.WriteLine("Hello, World!");
      }
   }
}
----------------------------------------

This was simple enough to compile with:

    csc /t:library Brandon.cs

which produced a "Brandon.dll" file.  Look at what happens if I then try
importing that DLL from Python for .NET:

    >>> import clr
    >>> print clr.FindAssembly('Brandon')
    C:\Users\brandon\dev\pythonnet\Brandon.dll
    >>> clr.AddReference('Brandon')
    ...
    System.IO.FileNotFoundException: Unable to find assembly 'Brandon'.
       at Python.Runtime.CLRModule.AddReference(String name) in c:\Users\brandon\dev\pythonnet\src\runtime\moduleobject.cs:line 370

You can see that, oddly enough, Python can find the assembly, but cannot
create a reference to it.  This error message also results, by the way,
if I compile "Brandon.cs" with a "/keyfile:..." option and run "gacutil
/i Brandon.dll" before trying to run my Python code.

I know that the step I am missing is probably blazingly obvious to those
who have been using Windows longer, :-) but I will very much appreciate
whomever who can point out what critical step I am missing in getting my
Python code to call C# code.  Other than this hangup, I'm impressed by
how quickly Python for .NET got me up and running!

-- 
Brandon Craig Rhodes   brandon at rhodesmill.org   http://rhodesmill.org/brandon


More information about the PythonDotNet mailing list