16

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.

3
  • 1
    What happens if you change the line to clr.AddReference("yxz"), does that make any difference? Jul 29, 2009 at 13:23
  • If the problem is that the DLL can't be found, use Process Monitor by Sysinternals to pinpoint what's wrong. Jul 29, 2009 at 13:49
  • 1
    fuslogvw.exe is also a good way to figure out why a managed DLL isn't found - it comes w/ the platform SDK. Jul 29, 2009 at 15:35

6 Answers 6

13
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
4
  • 3
    You can also use clr.AddReferenceToFileAndPath, which does exactly that.
    – Jeff Hardy
    Jan 10, 2013 at 16:35
  • 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. Jul 27, 2015 at 14:26
  • I tried this, but getting an error: AttributeError: 'module' object has no attribute 'AddReferenceToFileAndPath'
    – Avis
    Feb 23, 2017 at 13:12
  • The dll in clr.AddReference() should be without the .dll extension
    – Alaa M.
    Dec 2, 2019 at 11:45
9

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.

4

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.

2
  • 2
    use import sys print sys.path to determine where clr.AddReference is looking for dlls
    – N_A
    Jul 3, 2012 at 14:47
  • 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. Mar 8, 2013 at 11:28
1

You can use this:

import clr 
clr.AddReferenceToFile("yxz.dll")
0

It's better to use clr.AddReferenceToFile(filename) , because it takes a relative path.

import clr 
clr.AddReferenceToFile("xxx.dll")

Then you can import the classes by import as usual:

import xxx

or

from xxx import *

I recommend you to check out this book , it's very helpful. https://play.google.com/store/apps/details?id=com.gavin.gbook

1
  • AddReferenceToFileAndPath also works with relative directory paths, fwiw. Jan 16, 2015 at 17:32
0

I got 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.

As a bit of a hacky solution, I created a file fixpath.py

"""This hacky script fixes the sys.path when I run the ipy console."""

import sys
import os

sys.path.insert(0, os.getcwd())

del sys
del os

Then I set up an environment variable IRONPYTHONSTARTUP with the absolute path to this file. Then whenever I start my IronPython console, this script is run and my sys.path includes an absolute reference to my current working directory and the subsequent calls to clr.AddReference work properly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.