Code Yarns ‍👨‍💻
Tech BlogPersonal Blog

How to add a .Net assembly as reference in IronPython

📅 2013-Jun-07 ⬩ ✍️ Ashwin Nanjappa ⬩ 🏷️ assembly, dotnet, ironpython ⬩ 📚 Archive


By installing IronPython and Python Tools for Visual Studio, I can work with .Net using Python. The typical method of adding a .Net assembly file as a Reference is to use choose Add Reference in Visual Studio. But, doing this did not work for importing a .Net assembly built using C#. The assembly appeared in the References list, but had a Warning sign over it. And sure enough, importing a namespace that the assembly exports failed with a ImportException.


Apparently, the Add Reference method only works for Python Extension Modules, not for .Net assemblies written in C#. Instead use the methods in clr module to add the reference:

  1. Place the .Net assembly file, say Foo.dll, and any other DLL files that it is dependent on in the directory where your IronPython source file lies.

  2. Add a reference to this .Net assembly file in your IronPython using and import the namespace exported by the .Net assembly in your IronPython code.

# Add reference to .Net assembly named Foo.dll
import clr
import Foo

Note: Remember to place all the DLL files that the .Net assembly is dependent on along with it in the same directory. Else you will get a FileNotFoundException.

Tried with: IronPython 2.7.3, Visual Studio 2012 and Windows 7 x64

© 2023 Ashwin Nanjappa • All writing under CC BY-SA license • 🐘 Mastodon📧 Email