[Python.NET] Using Third Party C# Libraries

J. Merrill jvm_cop at spamcop.net
Wed Feb 21 01:06:09 CET 2007


Even if you get a 1.1-compatible version of the DLL, or you build Python.Net 2 from source, you will need to learn the namespace(s) and name(s) of the C# class(es) you want to use.  From what you have below, it seems that the LoadWithPartialName call worked (so you don't need to copy the DLL to everywhere in the file system) but the import didn't.  

You need to know the name of the C# namespace and class to import -- as Maksim wrote
   from CLR.Name.Space import ClassName
For you that would probably be something like
   from CLR.SharpNeatVendorName.SharpNeat import MainClass
assuming that they follow the convention of putting their work in a namespace under a namespace based on their company/organization name.

The magic of Python.Net's CLR module is that it makes the C# DLL available dynamically -- but you have to know the namespace(s) and class(es).

At 12:13 PM 2/20/2007, Chris Spencer wrote
>I'm assuming you meant I have to replace 'TheAssembly' with some
>variation of SharpNeatLib.dll. Could you please be more specific? It
>doesn't seem to work for any combination.
>
>import CLR
>from CLR.System.Reflection import Assembly
>Assembly.LoadWithPartialName('SharpNeatLib')
>import CLR.SharpNeatLib
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>ImportError: No module named SharpNeatLib
>
>import CLR
>from CLR.System.Reflection import Assembly
>Assembly.LoadWithPartialName('SharpNeatLib.dll')
>import CLR.SharpNeatLib
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>ImportError: No module named SharpNeatLib
>
>import CLR
>from CLR.System.Reflection import Assembly
>Assembly.LoadWithPartialName('SharpNeat')
>import CLR.SharpNeat
>Traceback (most recent call last):
>  File "<stdin>", line 1, in ?
>ImportError: No module named SharpNeat
>
>Regards,
>Chris
>
>On 2/20/07, Maksim Kozyarchuk <mkozyarchuk at funddevelopmentservices.com> wrote:
>> Try the following syntax.
>>
>> import CLR
>> from CLR.System.Reflection import Assembly
>> Assembly.LoadWithPartialName('TheAssebly')
>> from CLR.Name.Space import ClassName
>>
>>
>>
>>
>>
>> -----Original Message-----
>> From: pythondotnet-bounces at python.org
>> [mailto:pythondotnet-bounces at python.org] On Behalf Of Chris Spencer
>> Sent: Tuesday, February 20, 2007 11:55 AM
>> To: pythondotnet at python.org
>> Subject: [Python.NET] Using Third Party C# Libraries
>>
>> Hi,
>>
>> I'm new to Python.Net, and I'd like to access a C# library (SharpNeat)
>> from Python. I tried placing it's DLL (SharpNeatLib.dll) into
>> C:\Program Files\PythonNet\Lib\site-packages but  when I try to import
>> it I get the error:
>>
>> >>> import SharpNeatLib
>> Traceback (most recent call last):
>>   File "<stdin>", line 1, in ?
>> ImportError: dynamic module does not define init function
>> (initSharpNeatLib)
>>
>> Is there something I'm doing wrong? Any help is appreciated.
>>
>> Thanks,
>> Chris


J. Merrill / Analytical Software Corp




More information about the PythonDotNet mailing list