How to use a C# dll in IronPython

35,469

Solution 1

import clr    
clr.AddReferenceToFileAndPath(r"C:\Folder\Subfolder\file.dll")

is the simplest way as proposed by Jeff in the comments. This also works:

import clr
import sys

sys.path.append(r"C:\Folder\Subfolder")  # path of dll
clr.AddReference ("Ipytest.dll") # the dll
import TestNamspace  # import namespace from Ipytest.dll

Solution 2

I think it's failing to find the file because it doesn't know where to look for it, see here for a detailed explanation as to how the clr.AddReference...() functions work.

Solution 3

The Creating .NET Classes Dynamically from IronPython example, creates an assembly (which is later saved to disk as "DynamicAsm.dll"). It contains a class called "DynamicType", with a single static method called 'test'. This method takes four integers and adds them together.

The nice thing is that this saves "DynamicAsm.dll" to disk. You can then start an IronPython interactive interpreter session and do the following :

>>> import clr
>>> clr.AddReference('DynamicAsm.dll')
>>> import DynamicType
>>> DynamicType.test(2, 3, 4, 5)
14

Note that the example uses the class name in the import statement.

Solution 4

You can use this:

import clr 
clr.AddReferenceToFile("yxz.dll")
Share:
35,469
Admin
Author by

Admin

Updated on July 13, 2022

Comments

  • Admin
    Admin almost 2 years

    I have created a dll using C#. How do use the dll in IronPython. I have tried to add the dll using clr.AddReference("yxz.dll"). But it fails. I have tried placing the dll in the execution directory of the IronPython script. Still it fails stating that "Name xyz cannot be found" while trying to refer the dll.

  • N_A
    N_A almost 12 years
    use import sys print sys.path to determine where clr.AddReference is looking for dlls
  • Jeff Hardy
    Jeff Hardy over 11 years
    You can also use clr.AddReferenceToFileAndPath, which does exactly that.
  • Hendrik Wiese
    Hendrik Wiese over 11 years
    Can I actually do this from the outside? Or do I really have to import clr and add the reference to the assembly in my IronPython script? I've got some assemblies here that I'd like to have imported statically so that they are always available in the script.
  • EdwardG
    EdwardG over 9 years
    AddReferenceToFileAndPath also works with relative directory paths, fwiw.
  • Aaron Milner
    Aaron Milner almost 9 years
    I get this behaviour only from the IronPython console. When I run a script it is fine. When I run a script the IronPython, sys.path contains an absolute path to my current working directory so it works. When I type in the console, sys.path only includes a '.' for the current working directory. That may explain the difference in behaviour.
  • Avis
    Avis over 7 years
    I tried this, but getting an error: AttributeError: 'module' object has no attribute 'AddReferenceToFileAndPath'
  • Alaa M.
    Alaa M. over 4 years
    The dll in clr.AddReference() should be without the .dll extension