9

I am using Windows 7, 64-bit. I have managed to download and install pythonnet, so

import clr
clr.AddReference("System.Windows.Forms")
from System.Windows.Forms import Form

works fine. I have also downloaded and compiled/run a C# application which creates lots of assemblies. The application in question is ARDrone-Control-.NET.

How can I use the generated DLL files from Python (and not just the built-in C# classes).

Since I have never used C# (which is why I want to use the library from Python), I'd be happy to clarify the question.

denfromufa
  • 5,610
  • 13
  • 81
  • 138
Petter
  • 37,121
  • 7
  • 47
  • 62
  • 1
    It seems like you're getting a lot of unnecessary friction. Have you considered simply using Iron Python for this project? – Paul Sasik Jun 13 '11 at 16:12
  • IronPython is pretty stable, but development became very slow once open-sourced by MS. CPython is very stable and has powerful C API, used by numpy, Cython, pandas, etc.. – denfromufa Nov 07 '14 at 04:04

2 Answers2

13

Just to provide another method:

import sys
sys.path.append("C:\Path\to\your\assemblies")

clr.AddReference('MyAssembly')
from MyAssembly import MyClass

MyClass.does_something()

This assumes that in the C:\Path\to\your\assemblies folder you have a MyAssembly.dll file.

So the 'trick' is that you have to add your assemblies folder to the sys.path before clr.AddReference.

John
  • 1,263
  • 2
  • 15
  • 26
  • I had to add the assembly directory to sys.path before I imported clr. I don't know if that's recent, or an anomaly with my machine - but if it is important, it should perhaps be noted here. – Tritium21 May 08 '16 at 15:04
3

From what I gather you are trying to load an external assembly in Python.Net, I have done little work with that library. You should consider using IronPython instead but using Python.Net you could load the assembly via .Net's reflection like this

Python 2.7.13 (v2.7.13:a06454b1afa1, Dec 17 2016, 20:42:59) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import clr
>>> from System.Reflection import Assembly
>>> dll1 = Assembly.LoadFile("C:\Python\Python27-32\Lib\site-packages\Python.Runtime.dll")
>>> clr.Python.Runtime
<module 'Python.Runtime'>
>>> clr.Python.Runtime.PythonEngine
<class 'Python.Runtime.PythonEngine'>
denfromufa
  • 5,610
  • 13
  • 81
  • 138
mitchellsg
  • 486
  • 5
  • 13