accessing .net dlls from python

Dear All I'm interested in accessing .net dlls from python (e.g. to write test scripts). Is pythonnet the correct software to use? I have had a go. I have installed pythonnet and tested it with some of the code samples: all well. Putting a .net dll called foo.dll in the current directory, 'import foo' raises the following error: ImportError: dynamic module does not define init function (initfoo) Is there a liitle bit extra I need to do, or am I barking up the wrong tree? I suppose I'm looking for something that will allow me to import .net dlls as if they were python modules. Any advice much appreciated. Best wishes Ivan

On 3/28/06, ivan Uemlianin <i.uemlianin@bangor.ac.uk> wrote:
Dear All
I'm interested in accessing .net dlls from python (e.g. to write test scripts). Is pythonnet the correct software to use?
Definitely.
I have had a go. I have installed pythonnet and tested it with some of the code samples: all well. Putting a .net dll called foo.dll in the current directory, 'import foo' raises the following error:
ImportError: dynamic module does not define init function (initfoo)
To Brad: I think it could be nice idea to write custom import that will load .net dll automatically. I will see what can I do. The main question is how to behave with top level namespaces.
Is there a liitle bit extra I need to do, or am I barking up the wrong tree? I suppose I'm looking for something that will allow me to import .net dlls as if they were python modules.
I could be wrong, but it seems to me that before importing something from .net dlls, you should load them first. import CLR from CLR.System.Reflection import Assembly my_dll_name = Assembly.LoadWithPartialName( path to dll that does not include .dll extension ) #import namespaces from CLR import TopNamespace
Any advice much appreciated.
I hope this was helpful.
Best wishes
Ivan
-- Roman Yakovenko C++ Python language binding http://www.language-binding.net/

Roman Yakovenko wrote:
On 3/28/06, ivan Uemlianin <i.uemlianin@bangor.ac.uk> wrote:
Is there a litle bit extra I need to do, or am I barking up the wrong tree? I suppose I'm looking for something that will allow me to import .net dlls as if they were python modules.
I could be wrong, but it seems to me that before importing something from .net dlls, you should load them first.
import CLR from CLR.System.Reflection import Assembly
my_dll_name = Assembly.LoadWithPartialName( path to dll that does not include .dll extension )
#import namespaces from CLR import TopNamespace
Hernan Martinez Foffani wrote:
http://www.zope.org/Members/Brian/PythonNet/readme.html#importing
Aha! Thanks! This has moved me on a lot, but I don't think I'm quite there yet: 1. Installing Python for .NET as a standalone, and using it in the interactive session, all of the above, apart from 'from CLR import TopNamespace' works. 'from CLR import TopNamespace' gets 'ImportError: cannot import name TopNamespace'. 2. Installing Python for .NET as part of my existing Python installation, and using it from within Pythonwin, none of the above works: 'import CLR' raises a 'System.IO.FileNotFoundException' - I am alerted by a 'Just-In-Time Debugging' pop-up window and Pythonwin terminates 'in an unusual way'. Should Python for .NET be used only from its own interactive prompt? Why can't it import TopNamespace and can I do without it? Thanks again for your help. This is going to be very handy: Windows and .NET are alien environments to me (I work with linux and python). Best Ivan

import CLR from CLR.System.Reflection import Assembly
my_dll_name = Assembly.LoadWithPartialName( path to dll that does not include .dll extension )
#import namespaces from CLR import TopNamespace
1. Installing Python for .NET as a standalone, and using it in the interactive session, all of the above, apart from 'from CLR import TopNamespace' works. 'from CLR import TopNamespace' gets 'ImportError: cannot import name TopNamespace'.
If your C# source file contains something along the lines of: namespace MyLib { public class MyClass { ... } } and, say, is compiled to a DLL called A_LIB.DLL and placed in your current directory, then in python.net it would be: from CLR.System.Reflection import Assembly a = Assembly.LoadWithPartialName("A_LIB") from CLR.MyLib import MyClass -H.

Ivan Uemlianin wrote:
Roman Yakovenko wrote:
On 3/28/06, ivan Uemlianin <i.uemlianin@bangor.ac.uk> wrote:
Is there a litle bit extra I need to do, or am I barking up the wrong tree? I suppose I'm looking for something that will allow me to import .net dlls as if they were python modules.
I could be wrong, but it seems to me that before importing something from .net dlls, you should load them first.
import CLR
from CLR.System.Reflection import Assembly
my_dll_name = Assembly.LoadWithPartialName( path to dll that does not include .dll extension )
#import namespaces
from CLR import TopNamespace
1. Installing Python for .NET as a standalone, and using it in the interactive session, all of the above, apart from 'from CLR import TopNamespace' works. 'from CLR import TopNamespace' gets 'ImportError: cannot import name TopNamespace'.
Duh. Of course I replace 'TopNamespace' with my own namespace. Please regard this question as a typo. Best Ivan

I'm interested in accessing .net dlls from python (e.g. to write test scripts). Is pythonnet the correct software to use?
Yes.
I have had a go. I have installed pythonnet and tested it with some of the code samples: all well. Putting a .net dll called foo.dll in the current directory, 'import foo' raises the following error:
ImportError: dynamic module does not define init function (initfoo)
Is there a liitle bit extra I need to do, or am I barking up the wrong tree? I suppose I'm looking for something that will allow me to import .net dlls as if they were python modules.
http://www.zope.org/Members/Brian/PythonNet/readme.html#importing -H.
participants (4)
-
Hernan Martinez Foffani
-
ivan Uemlianin
-
Ivan Uemlianin
-
Roman Yakovenko